pythonselenium-webdriverseleniumbasecloudflare-turnstile

SeleniumBase example failing Cloudflare Turnstile


I'm encountering an issue with SeleniumBase while trying to automate a form submission that includes a Cloudflare Turnstile CAPTCHA. The script fails with a NoSuchElementException when attempting to find an iframe element after waiting for 7 seconds.

https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_form_turnstile.py

from seleniumbase import SB

with SB(uc=True, test=True) as sb:
    url = "seleniumbase.io/apps/form_turnstile"
    sb.uc_open_with_reconnect(url, 2)
    sb.press_keys("#name", "SeleniumBase")
    sb.press_keys("#email", "test@test.test")
    sb.press_keys("#phone", "1-555-555-5555")
    sb.click('[for="date"]')
    sb.click("td.is-today button")
    sb.click('div[class="select-wrapper"] input')
    sb.click('span:contains("9:00 PM")')
    sb.highlight_click('input[value="AR"] + span')
    sb.click('input[value="cc"] + span')
    sb.scroll_to("iframe")
    sb.uc_gui_handle_cf()
    sb.highlight("img#captcha-success", timeout=3)
    sb.highlight_click('button:contains("Request & Pay")')
    sb.highlight("img#submit-success")
    sb.highlight('button:contains("Success!")')

The error message I'm getting is:

seleniumbase.common.exceptions.NoSuchElementException: Message: Element {iframe} was not present after 7 seconds!

Any insights or suggestions on how to handle this iframe issue with Cloudflare's Turnstile CAPTCHA would be greatly appreciated!


Solution

  • Try the newer version with seleniumbase 4.28.7 (or newer):

    from seleniumbase import SB
    
    with SB(uc=True, test=True) as sb:
        url = "seleniumbase.io/apps/form_turnstile"
        sb.uc_open_with_reconnect(url, 2)
        sb.press_keys("#name", "SeleniumBase")
        sb.press_keys("#email", "test@test.test")
        sb.press_keys("#phone", "1-555-555-5555")
        sb.click('[for="date"]')
        sb.click("td.is-today button")
        sb.click('div[class="select-wrapper"] input')
        sb.click('span:contains("9:00 PM")')
        sb.highlight_click('input[value="AR"] + span')
        sb.click('input[value="cc"] + span')
        sb.scroll_to("div.cf-turnstile-wrapper")
        sb.uc_gui_handle_cf()
        sb.highlight("img#captcha-success", timeout=3)
        sb.highlight_click('button:contains("Request & Pay")')
        sb.highlight("img#submit-success")
        sb.highlight('button:contains("Success!")')
    

    There was a small change to the script after Cloudflare hid the iframe in a closed Shadow Root. (https://github.com/seleniumbase/SeleniumBase/commit/f007dcf607293c42dd2b36a76339c2dd68ec8b7f). Also, seleniumbase 4.28.7 made changes due to other updates that Cloudflare made.