Skip to content

Commit 94c2b5e

Browse files
committed
Improve methods that scroll to elements
1 parent d2e2b80 commit 94c2b5e

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12252,7 +12252,7 @@ def __click_with_offset(
1225212252
self.execute_script(script)
1225312253
try:
1225412254
element_location = element.location["y"]
12255-
element_location = element_location - 130 + y
12255+
element_location = element_location - constants.Scroll.Y_OFFSET + y
1225612256
if element_location < 0:
1225712257
element_location = 0
1225812258
scroll_script = "window.scrollTo(0, %s);" % element_location
@@ -12352,8 +12352,8 @@ def __jquery_slow_scroll_to(self, selector, by="css selector"):
1235212352
selector = self.__make_css_match_first_element_only(selector)
1235312353
scroll_script = (
1235412354
"""jQuery([document.documentElement, document.body]).animate({"""
12355-
"""scrollTop: jQuery('%s').offset().top - 130}, %s);"""
12356-
% (selector, scroll_time_ms)
12355+
"""scrollTop: jQuery('%s').offset().top - %s}, %s);"""
12356+
% (selector, constants.Scroll.Y_OFFSET, scroll_time_ms)
1235712357
)
1235812358
if js_utils.is_jquery_activated(self.driver):
1235912359
self.execute_script(scroll_script)

seleniumbase/fixtures/constants.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ class Values:
138138
# Demo Mode has slow scrolling to see where you are on the page better.
139139
# However, a regular slow scroll takes too long to cover big distances.
140140
# If the scroll distance is greater than SSMD, a slow scroll speeds up.
141-
SSMD = 900 # Smooth Scroll Minimum Distance (for advanced slow scroll)
141+
SSMD = 500 # Smooth Scroll Minimum Distance (for advanced slow scroll)
142+
143+
144+
class Scroll:
145+
Y_OFFSET = 182
142146

143147

144148
class Warnings:

seleniumbase/fixtures/js_utils.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ def get_scroll_distance_to_element(driver, element):
10021002
scroll_position = driver.execute_script("return window.scrollY;")
10031003
element_location = None
10041004
element_location = element.location["y"]
1005-
element_location = element_location - 130
1005+
element_location = element_location - constants.Scroll.Y_OFFSET
10061006
if element_location < 0:
10071007
element_location = 0
10081008
distance = element_location - scroll_position
@@ -1026,7 +1026,7 @@ def scroll_to_element(driver, element):
10261026
screen_width = driver.get_window_size()["width"]
10271027
except Exception:
10281028
element_location_x = 0
1029-
element_location_y = element_location_y - 130
1029+
element_location_y = element_location_y - constants.Scroll.Y_OFFSET
10301030
if element_location_y < 0:
10311031
element_location_y = 0
10321032
element_location_x_fix = element_location_x - 400
@@ -1052,16 +1052,27 @@ def slow_scroll_to_element(driver, element, browser):
10521052
scroll_to_element(driver, element)
10531053
return
10541054
scroll_position = driver.execute_script("return window.scrollY;")
1055-
element_location = None
1055+
element_location_y = None
10561056
try:
1057-
element_location = element.location["y"]
1057+
element_location_y = element.location["y"]
10581058
except Exception:
10591059
element.location_once_scrolled_into_view
10601060
return
1061-
element_location = element_location - 130
1062-
if element_location < 0:
1063-
element_location = 0
1064-
distance = element_location - scroll_position
1061+
try:
1062+
element_location_x = element.location["x"]
1063+
element_width = element.size["width"]
1064+
screen_width = driver.get_window_size()["width"]
1065+
except Exception:
1066+
element_location_x = 0
1067+
element_location_y = element_location_y - constants.Scroll.Y_OFFSET
1068+
if element_location_y < 0:
1069+
element_location_y = 0
1070+
element_location_x_fix = element_location_x - 400
1071+
if element_location_x_fix < 0:
1072+
element_location_x_fix = 0
1073+
if element_location_x + element_width <= screen_width:
1074+
element_location_x_fix = 0
1075+
distance = element_location_y - scroll_position
10651076
if distance != 0:
10661077
total_steps = int(abs(distance) / 50.0) + 2.0
10671078
step_value = float(distance) / total_steps
@@ -1072,7 +1083,9 @@ def slow_scroll_to_element(driver, element, browser):
10721083
scroll_script = "window.scrollTo(0, %s);" % new_position
10731084
driver.execute_script(scroll_script)
10741085
time.sleep(0.01)
1075-
scroll_script = "window.scrollTo(0, %s);" % element_location
1086+
scroll_script = "window.scrollTo(%s, %s);" % (
1087+
element_location_x_fix, element_location_y
1088+
)
10761089
driver.execute_script(scroll_script)
10771090
time.sleep(0.01)
10781091
if distance > 430 or distance < -300:

0 commit comments

Comments
 (0)