mirror of
https://github.com/User0332/rewards-farmer.git
synced 2026-09-16 01:31:36 +00:00
fix race condition when grabbing mouse position
This commit is contained in:
+12
-5
@@ -2,6 +2,7 @@ import time
|
|||||||
from selenium.webdriver.common.actions.action_builder import ActionBuilder
|
from selenium.webdriver.common.actions.action_builder import ActionBuilder
|
||||||
from selenium.webdriver.common.action_chains import ActionChains
|
from selenium.webdriver.common.action_chains import ActionChains
|
||||||
from selenium.webdriver.remote.webelement import WebElement
|
from selenium.webdriver.remote.webelement import WebElement
|
||||||
|
from selenium.common.exceptions import JavascriptException
|
||||||
from selenium import webdriver
|
from selenium import webdriver
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import math
|
import math
|
||||||
@@ -229,8 +230,8 @@ class MouseUtils:
|
|||||||
initial_pos = self.fallback_init_pos
|
initial_pos = self.fallback_init_pos
|
||||||
|
|
||||||
js_tracker = f"""
|
js_tracker = f"""
|
||||||
window.cursorX = {initial_pos[0]};
|
window.cursorX = {int(initial_pos[0])};
|
||||||
window.cursorY = {initial_pos[1]};
|
window.cursorY = {int(initial_pos[1])};
|
||||||
document.addEventListener('mousemove', function(event) {{
|
document.addEventListener('mousemove', function(event) {{
|
||||||
console.log('Mouse moved to: ' + event.clientX + ', ' + event.clientY);
|
console.log('Mouse moved to: ' + event.clientX + ', ' + event.clientY);
|
||||||
window.cursorX = event.clientX;
|
window.cursorX = event.clientX;
|
||||||
@@ -265,8 +266,9 @@ class MouseUtils:
|
|||||||
self.driver.execute_script(cursor_script)
|
self.driver.execute_script(cursor_script)
|
||||||
|
|
||||||
def get_current_mouse_position(self) -> Point:
|
def get_current_mouse_position(self) -> Point:
|
||||||
x = self.driver.execute_script("return window.cursorX;")
|
pos: dict[str, int] = self.driver.execute_script("return { x: window.cursorX, y: window.cursorY };")
|
||||||
y = self.driver.execute_script("return window.cursorY;")
|
|
||||||
|
x, y = pos['x'], pos['y']
|
||||||
|
|
||||||
if (x, y) == (None, None):
|
if (x, y) == (None, None):
|
||||||
self.reinitialize()
|
self.reinitialize()
|
||||||
@@ -292,7 +294,12 @@ class MouseUtils:
|
|||||||
|
|
||||||
self.fallback_init_pos = point
|
self.fallback_init_pos = point
|
||||||
|
|
||||||
if visualize: self.driver.execute_script(f"window.moveVisualCursor({point[0]}, {point[1]});")
|
if visualize:
|
||||||
|
try: self.driver.execute_script(f"window.moveVisualCursor({point[0]}, {point[1]});")
|
||||||
|
except JavascriptException: # some uninitialization has happened, reinitialize the cursor visualization
|
||||||
|
self.reinitialize()
|
||||||
|
self.driver.execute_script(f"window.moveVisualCursor({point[0]}, {point[1]});")
|
||||||
|
|
||||||
|
|
||||||
def move_to_element(self, element: WebElement, visualize: bool=True):
|
def move_to_element(self, element: WebElement, visualize: bool=True):
|
||||||
current_mouse_position = self.get_current_mouse_position()
|
current_mouse_position = self.get_current_mouse_position()
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from selenium.webdriver.support.ui import WebDriverWait
|
|||||||
from selenium.webdriver.common.keys import Keys
|
from selenium.webdriver.common.keys import Keys
|
||||||
from selenium.webdriver.remote.webelement import WebElement
|
from selenium.webdriver.remote.webelement import WebElement
|
||||||
from selenium.webdriver.common.action_chains import ActionChains
|
from selenium.webdriver.common.action_chains import ActionChains
|
||||||
|
from selenium.common.exceptions import StaleElementReferenceException
|
||||||
import tab_utils
|
import tab_utils
|
||||||
import llm_utils
|
import llm_utils
|
||||||
import mouse_trajectory
|
import mouse_trajectory
|
||||||
@@ -168,14 +169,17 @@ class RewardsTaskUtils:
|
|||||||
|
|
||||||
for i, query in enumerate(
|
for i, query in enumerate(
|
||||||
llm_utils.get_related_search_queries(
|
llm_utils.get_related_search_queries(
|
||||||
llm_utils.get_random_noun(), num_queries=searches_needed
|
llm_utils.get_random_noun(), num_queries=20
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
self.keyboard.send_keys(query+Keys.ENTER)
|
self.keyboard.send_keys(query+Keys.ENTER)
|
||||||
|
|
||||||
time.sleep(random.uniform(0.5, 1))
|
time.sleep(random.uniform(0.5, 1))
|
||||||
|
|
||||||
self.move_to_and_click(self.elements.get_clear_bing_search_query_button())
|
try: self.wait_for_then_click(self.elements.get_clear_bing_search_query_button)
|
||||||
|
except StaleElementReferenceException:
|
||||||
|
print(f"[WARNING] StaleElementReferenceException when trying to click the clear button for query {i+1}. Trying again...")
|
||||||
|
self.wait_for_then_click(self.elements.get_clear_bing_search_query_button)
|
||||||
|
|
||||||
self.driver.get("https://rewards.bing.com/")
|
self.driver.get("https://rewards.bing.com/")
|
||||||
self.tab_utils.ensure_focus()
|
self.tab_utils.ensure_focus()
|
||||||
|
|||||||
Reference in New Issue
Block a user