# Selenium Usage

## Sync Usage (Selenium)

```python
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)

```python
import asyncio
from selenium_driverless import webdriver
from selenium_driverless.types.by import By
from cdp_patches.input import AsyncInput

async def main():
    async with webdriver.Chrome(...) as driver:
        async_input = await AsyncInput(browser=driver)
    
        # Example: Click Button
        # Find Button Coords
        locator = await driver.find_element(By.XPATH, "//button")
        x, y = await locator.mid_location()
        # Click Coords => Click Button
        await async_input.click("left", x, y)

asyncio.run(main())
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vinyzu.gitbook.io/cdp-patches-documentation/input/selenium-usage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
