Skip to content

Commit 8492b39

Browse files
committed
Scrolling to elements should include horizontal scrolling
1 parent b00f75b commit 8492b39

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

seleniumbase/fixtures/js_utils.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -962,16 +962,31 @@ def get_scroll_distance_to_element(driver, element):
962962

963963

964964
def scroll_to_element(driver, element):
965-
element_location = None
965+
element_location_y = None
966+
element_location_x = None
967+
element_width = 0
968+
screen_width = 0
966969
try:
967-
element_location = element.location["y"]
970+
element_location_y = element.location["y"]
968971
except Exception:
969-
# element.location_once_scrolled_into_view # Old hack
970972
return False
971-
element_location = element_location - 130
972-
if element_location < 0:
973-
element_location = 0
974-
scroll_script = "window.scrollTo(0, %s);" % element_location
973+
try:
974+
element_location_x = element.location["x"]
975+
element_width = element.size["width"]
976+
screen_width = driver.get_window_size()["width"]
977+
except Exception:
978+
element_location_x = 0
979+
element_location_y = element_location_y - 130
980+
if element_location_y < 0:
981+
element_location_y = 0
982+
element_location_x_fix = element_location_x - 400
983+
if element_location_x_fix < 0:
984+
element_location_x_fix = 0
985+
if element_location_x + element_width <= screen_width:
986+
element_location_x_fix = 0
987+
scroll_script = "window.scrollTo(%s, %s);" % (
988+
element_location_x_fix, element_location_y
989+
)
975990
# The old jQuery scroll_script required by=By.CSS_SELECTOR
976991
# scroll_script = "jQuery('%s')[0].scrollIntoView()" % selector
977992
try:

0 commit comments

Comments
 (0)