@@ -1002,7 +1002,7 @@ def get_scroll_distance_to_element(driver, element):
1002
1002
scroll_position = driver .execute_script ("return window.scrollY;" )
1003
1003
element_location = None
1004
1004
element_location = element .location ["y" ]
1005
- element_location = element_location - 130
1005
+ element_location = element_location - constants . Scroll . Y_OFFSET
1006
1006
if element_location < 0 :
1007
1007
element_location = 0
1008
1008
distance = element_location - scroll_position
@@ -1026,7 +1026,7 @@ def scroll_to_element(driver, element):
1026
1026
screen_width = driver .get_window_size ()["width" ]
1027
1027
except Exception :
1028
1028
element_location_x = 0
1029
- element_location_y = element_location_y - 130
1029
+ element_location_y = element_location_y - constants . Scroll . Y_OFFSET
1030
1030
if element_location_y < 0 :
1031
1031
element_location_y = 0
1032
1032
element_location_x_fix = element_location_x - 400
@@ -1052,16 +1052,27 @@ def slow_scroll_to_element(driver, element, browser):
1052
1052
scroll_to_element (driver , element )
1053
1053
return
1054
1054
scroll_position = driver .execute_script ("return window.scrollY;" )
1055
- element_location = None
1055
+ element_location_y = None
1056
1056
try :
1057
- element_location = element .location ["y" ]
1057
+ element_location_y = element .location ["y" ]
1058
1058
except Exception :
1059
1059
element .location_once_scrolled_into_view
1060
1060
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
1065
1076
if distance != 0 :
1066
1077
total_steps = int (abs (distance ) / 50.0 ) + 2.0
1067
1078
step_value = float (distance ) / total_steps
@@ -1072,7 +1083,9 @@ def slow_scroll_to_element(driver, element, browser):
1072
1083
scroll_script = "window.scrollTo(0, %s);" % new_position
1073
1084
driver .execute_script (scroll_script )
1074
1085
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
+ )
1076
1089
driver .execute_script (scroll_script )
1077
1090
time .sleep (0.01 )
1078
1091
if distance > 430 or distance < - 300 :
0 commit comments