update: using global variables in 'selenium' module

This commit is contained in:
2026-04-22 17:02:00 +08:00
parent 5c13c1f7f9
commit 674e45712e
3 changed files with 19 additions and 5 deletions

View File

@@ -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)