seleniumbase / SeleniumBase

📊 Blazing fast Python framework for web crawling, scraping, testing, and reporting. Supports pytest. Stealth abilities: UC Mode and CDP Mode.
https://seleniumbase.io
MIT License
5.44k stars 983 forks source link

Need Help Handling Conditional CAPTCHA Challenges with uc_gui_click_captcha() #3276

Closed officicalalkhaldi closed 6 days ago

officicalalkhaldi commented 6 days ago

Hi, I’m using SeleniumBase on Windows, and I’m having trouble figuring out how to handle websites that sometimes throw a CAPTCHA challenge (like Cloudflare Turnstile).

Here’s what I’m trying to do:

I’ve written a script to open a website and click the CAPTCHA using uc_gui_click_captcha() method. But the problem is that I don’t know how to detect if the CAPTCHA is actually present before calling that method. Sometimes the challenge doesn’t show up, and I don’t want the script to break or waste time trying to click a CAPTCHA that’s not there.

Here’s the script I’m using:

import time
from seleniumbase import Driver

with Driver(
    uc=True,
    binary_location=r"C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe",
    agent=r"Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
    undetectable=True,
) as driver:
    driver.uc_open_with_reconnect(
        "https://2captcha.com/demo/cloudflare-turnstile-challenge", 4
    )
    driver.uc_gui_click_captcha()
    time.sleep(60)
    driver.quit()

f there’s a better way to handle this scenario, maybe an if statement It’d valuable to me and maybe to others, I really appreciate any guidance.

Thanks in advance!

mdmintz commented 6 days ago

The uc_gui_click_captcha() method automatically checks to see if there's a CAPTCHA before trying to click one. If there's no CAPTCHA on the page, then nothing happens when that method is called.

officicalalkhaldi commented 6 days ago

Ok, sounds good.

Thanks in advance !