@@ -962,16 +962,31 @@ def get_scroll_distance_to_element(driver, element):
962
962
963
963
964
964
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
966
969
try :
967
- element_location = element .location ["y" ]
970
+ element_location_y = element .location ["y" ]
968
971
except Exception :
969
- # element.location_once_scrolled_into_view # Old hack
970
972
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
+ )
975
990
# The old jQuery scroll_script required by=By.CSS_SELECTOR
976
991
# scroll_script = "jQuery('%s')[0].scrollIntoView()" % selector
977
992
try :
0 commit comments