From 674e45712e48c86c1d8341c8b381d5f8332119cc Mon Sep 17 00:00:00 2001 From: break27 Date: Wed, 22 Apr 2026 17:02:00 +0800 Subject: [PATCH] update: using global variables in 'selenium' module --- pyproject.toml | 2 +- src/common/utils/__init__.py | 3 ++- src/common/utils/selenium.py | 19 ++++++++++++++++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 573b636..9c1b9da 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" [project] name = "common" description = "Reusable code stubs" -version = "0.1.11" +version = "0.1.12" requires-python = ">=3.13" authors = [ { name="BreakerBear", email="breakerbear@autistic.men" }, diff --git a/src/common/utils/__init__.py b/src/common/utils/__init__.py index a318ed7..10337fb 100644 --- a/src/common/utils/__init__.py +++ b/src/common/utils/__init__.py @@ -1,5 +1,5 @@ from common.utils.timer import Timer -from common.utils.selenium import until, sleep, locate, click +from common.utils.selenium import until, sleep, locate, click, setup __all__ = [ 'Timer', @@ -7,4 +7,5 @@ __all__ = [ 'sleep', 'locate', 'click', + 'setup', ] diff --git a/src/common/utils/selenium.py b/src/common/utils/selenium.py index d12fb16..772873b 100644 --- a/src/common/utils/selenium.py +++ b/src/common/utils/selenium.py @@ -9,7 +9,10 @@ from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.remote.webelement import WebElement -def until(driver: WebDriver, condition: typing.Callable[[WebDriver], bool], timeout: float, watch=True): +driver: WebDriver|None = None +attempts, timeout, interval = int(), float(), float() + +def until(condition: typing.Callable[[WebDriver], bool], watch=True): try: WebDriverWait(driver, timeout).until(condition) except (TimeoutException, StaleElementReferenceException): @@ -23,7 +26,7 @@ def sleep(driver: WebDriver, seconds: float): try: WebDriverWait(driver, seconds, seconds).until(lambda _: False) except: pass -def locate(driver: WebDriver, selector: str, timeout: float, wait=True, condition=None) -> WebElement: +def locate(selector: str, wait=True, condition=None) -> WebElement: while True: try: locator = (By.CSS_SELECTOR, selector) @@ -41,7 +44,7 @@ def locate(driver: WebDriver, selector: str, timeout: float, wait=True, conditio except StaleElementReferenceException: pass -def click(driver: WebDriver, selector: str|WebElement, attempts: int, interval: int, wait=True, condition=None): +def click(selector: str|WebElement, wait=True, condition=None): predicate = condition if condition is not None else EC.element_to_be_clickable identity = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8)) element = locate(selector, wait, predicate) if isinstance(selector, str) else selector @@ -65,3 +68,13 @@ def click(driver: WebDriver, selector: str|WebElement, attempts: int, interval: break except TimeoutException: continue except: break + +def setup(a: WebDriver, b: int, c: float, d: float): + global driver, attempts, timeout, interval + driver = a + attempts = b + timeout = c + interval = d + + from common.jsonrpc2.server import connection + until(lambda _: connection is not None, watch=False)