Selenium Usage

Sync Usage (Selenium)

from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.by import By
from cdp_patches.input import SyncInput

# Locator Position Helper
def get_locator_pos(locator: WebElement):
    location = locator.location
    size = locator.size
    assert location, size

    x, y, width, height = location.get("x"), location.get("y"), size.get("width"), size.get("height")
    assert x and y and width and height

    x, y = x + width // 2, y + height // 2
    return x, y

options = webdriver.ChromeOptions()
# disable logs & automation
options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
options.add_argument("--log-level=3")

with webdriver.Chrome(...) as driver:
    sync_input = SyncInput(browser=driver)

    # Example: Click Button
    # Find Button Coords
    locator = driver.find_element(By.XPATH, "//button")
    x, y = get_locator_pos(locator)
    # Click Coords => Click Button
    sync_input.click("left", x, y)

Async Usage (Async Selenium-Driverless)

Last updated