mirror of
https://github.com/User0332/rewards-farmer.git
synced 2026-09-16 01:31:36 +00:00
scroll elements into view before moving the pointer
The pointer is moved to viewport coordinates, so an element below the fold produced a target outside the window and the driver raised MoveTargetOutOfBoundsException. Observed on the daily set opener during a scheduled run. Also clamps the bezier path to the viewport.
This commit is contained in:
+20
-1
@@ -282,11 +282,21 @@ class MouseUtils:
|
|||||||
start_time = time.monotonic()
|
start_time = time.monotonic()
|
||||||
end_time = start_time + move_time
|
end_time = start_time + move_time
|
||||||
|
|
||||||
|
# The distorted bezier path can overshoot the window edge, which the
|
||||||
|
# driver rejects, so keep every sampled point inside the viewport.
|
||||||
|
viewport = self.driver.execute_script(
|
||||||
|
"return [window.innerWidth, window.innerHeight];"
|
||||||
|
)
|
||||||
|
max_x, max_y = int(viewport[0]) - 2, int(viewport[1]) - 2
|
||||||
|
|
||||||
while (current_time := time.monotonic()) < end_time:
|
while (current_time := time.monotonic()) < end_time:
|
||||||
t = current_time - start_time
|
t = current_time - start_time
|
||||||
point = path_function(t)
|
point = path_function(t)
|
||||||
|
|
||||||
point = (max(0, point[0]), max(0, point[1])) # ensure the point is not negative
|
point = (
|
||||||
|
min(max(0, point[0]), max_x),
|
||||||
|
min(max(0, point[1]), max_y)
|
||||||
|
)
|
||||||
|
|
||||||
actions = ActionBuilder(self.driver, duration=0)
|
actions = ActionBuilder(self.driver, duration=0)
|
||||||
actions.pointer_action.move_to_location(point[0], point[1])
|
actions.pointer_action.move_to_location(point[0], point[1])
|
||||||
@@ -302,6 +312,15 @@ class MouseUtils:
|
|||||||
|
|
||||||
|
|
||||||
def move_to_element(self, element: WebElement, visualize: bool=True):
|
def move_to_element(self, element: WebElement, visualize: bool=True):
|
||||||
|
# The pointer is moved to viewport coordinates, so an element below the
|
||||||
|
# fold yields a target outside the window and the driver rejects the move
|
||||||
|
# with MoveTargetOutOfBoundsException. Bring it into view first.
|
||||||
|
self.driver.execute_script(
|
||||||
|
"arguments[0].scrollIntoView({block: 'center', inline: 'center'});",
|
||||||
|
element
|
||||||
|
)
|
||||||
|
time.sleep(0.4)
|
||||||
|
|
||||||
current_mouse_position = self.get_current_mouse_position()
|
current_mouse_position = self.get_current_mouse_position()
|
||||||
|
|
||||||
rect = self.driver.execute_script("""
|
rect = self.driver.execute_script("""
|
||||||
|
|||||||
Reference in New Issue
Block a user