Skip to content

Selenium (Python)

Selenium runs JavaScript with driver.execute_script. The helper module’s FIND_JS and PUT_JS are function expressions, so a two-line adapter calls them with an argument and returns the result.

Terminal window
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 next to your script. Selenium Manager (part of Selenium) finds Chrome and a matching driver, or downloads them.

signup.py
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.

  • 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.