# Selenium (Python)

> Solve reCAPTCHA, hCaptcha, Turnstile and FunCaptcha in a Selenium WebDriver script (Python) with NoptCHA - find the parameters, solve with the v1 API, put the token back and submit.

Source: https://noptcha.com/docs/integrations/selenium-python/

[Selenium](https://www.selenium.dev/documentation/webdriver/) runs JavaScript with `driver.execute_script`. The [helper module](https://noptcha.com/docs/integrations.md#the-helper-modules)’s `FIND_JS` and `PUT_JS` are function expressions, so a two-line adapter calls them with an argument and returns the result.

## Setup

```sh
pip install selenium requests
export NOPTCHA_KEY=nop_test_...   # a test key: free, fixed answers
# no account yet? export NOPTCHA_KEY=keyless (keyless sandbox, same fixed answers)
```

Save [`noptcha_browser.py`](https://noptcha.com/docs/integrations.md#the-helper-modules) next to your script. Selenium Manager (part of Selenium) finds Chrome and a matching driver, or downloads them.

## Example

signup.py

```python
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from noptcha_browser import solve_on_page

URL = "https://example.com/signup"  # a page you are allowed to automate


def evaluate(driver):
    """Adapter for solve_on_page: run one of the helper's JavaScript functions in the page."""
    return lambda js, arg=None: driver.execute_script(f"return ({js})(arguments[0]);", arg)


options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)  # Selenium Manager finds or downloads Chrome and its driver
try:
    driver.get(URL)
    # ... fill in the form as usual ...

    result = solve_on_page(evaluate(driver))
    if result:
        print("solved", result["type"], "->", result["did"])

    old = driver.find_element(By.TAG_NAME, "html")
    driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
    # the form was posted and the next page loaded; while the old page is going away ChromeDriver can
    # report a generic error instead of a stale element, so keep waiting on those too
    WebDriverWait(driver, 30, ignored_exceptions=[WebDriverException]).until(EC.staleness_of(old))
    print(driver.current_url, driver.title)
finally:
    driver.quit()
```

`execute_script` passes `arguments[0]` into the page and returns the function’s result as Python values, so `solve_on_page` gets the same `[{"type", "params"}]` list as with the other frameworks.

## Notes

-   **Timing.** Find the captcha once the widget is on the page (wait for it with `WebDriverWait` if it appears later) and solve right before you submit: tokens expire after a couple of minutes.
-   **Frames.** For a widget inside an iframe, `driver.switch_to.frame(...)` first, then `driver.switch_to.default_content()` afterwards.
-   **reCAPTCHA v3.** `PUT_JS` makes `grecaptcha.execute()` resolve with the token, so the site’s own submit handler picks it up: click the submit button afterwards.

> **Note:** Tested with Selenium 4.54 and Chrome, against local pages that imitate each widget, with a test key.
