seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
4.46k stars 909 forks source link

Cloudflare Form Turnstile Failure #2792

Closed asibhossen897 closed 1 month ago

asibhossen897 commented 1 month ago

I'm using Driver class to automate login process on a website and even I'm using the uc mode, it does not seem to work properly. I've also tried with other classes, the result is the same. When the span of the iframe is clicked, it shows Failure. I've also tried manual clicking, it's the same. Here is the code

from seleniumbase import Driver

driver = Driver(uc=True, browser='Chrome')

# Reading the credentials
with open('creds.txt', 'r') as f:
    lines = f.read().splitlines()

mail = lines[0].strip()
password = lines[1].strip()

try:
    driver.maximize_window()
    driver.uc_open_with_reconnect("https://visa.vfsglobal.com/ind/en/pol/login", reconnect_time=20)
    driver.find_element("#onetrust-accept-btn-handler").click()

    # Typing the credentials
    driver.type("#mat-input-0", mail)
    driver.type("#mat-input-1", password)
    driver.sleep(5)

    driver.highlight_click(".mat-button-wrapper")
    driver.sleep(5)

    driver.switch_to_frame("iframe")
    driver.uc_click("span")

Here is the screenshot failure-cf-form-turnstile

mdmintz commented 1 month ago

This was all that was needed to bypass the CAPTCHA:

from seleniumbase import SB

with SB(uc=True,) as sb:
    sb.driver.uc_open_with_reconnect(
        "https://visa.vfsglobal.com/are/en/pol/login",
        reconnect_time=12
    )

As seen here: https://stackoverflow.com/a/77578982/7058266

Don't maximize the window in UC Mode. Only use uc_click when clicking anything on a page that tries to detect Selenium. Make sure the reconnect_time is long enough to bypass detection. On that website, the CAPTCHA can be bypassing without clicking inside the iframe, which means that if you still have to click it, it means that they already detected you.

asibhossen897 commented 1 month ago

In the first page, yes, it can be passed without clicking. But I forgot to mention that the problem occurs in the second page, where we need to input the OTP. And if they've detected me, then what can I do? Any tips? (I've also tried using proxies)

Also, the SB class can not bypass the CAPTCHA in my mentioned URL https://visa.vfsglobal.com/ind/en/pol/login. When I try with the Driver class, the first page's CAPTCHA is bypassed without clicking.

from seleniumbase import SB
import time

# Reading the credentials
with open('creds.txt', 'r') as f:
    lines = f.read().splitlines()

mail = lines[0].strip()
password = lines[1].strip()

with SB(uc=True,) as sb:
    sb.driver.uc_open_with_reconnect(
        "https://visa.vfsglobal.com/ind/en/pol/login",
        reconnect_time=20
    )
    sb.press_keys("#mat-input-0", mail)
    sb.press_keys("#mat-input-1", password)
    time.sleep(2)
    sb.highlight_click(".mat-button-wrapper")

    time.sleep(20)
mdmintz commented 1 month ago

Not sure what you're seeing, because I don't have a full screenshot of where your script got stuck, but for maximum stealth, combine UC Mode with pyautogui and perform actions while disconnected. You can do driver.disconnect() to disconnect the driver. Then perform the pyautogui actions. Then driver.connect() afterwards so that you can perform Selenium actions again.

asibhossen897 commented 1 month ago

Ok, thanks. I'll try this. What screenshot I need to provide here? If you don't mind me asking.

mdmintz commented 1 month ago

You said that the problem occurs on the second page, where you need to input the OTP. A screenshot might help, since I won't be able to see that page without having an account.

UC Mode should still work if used properly. If not, use pyautogui.

asibhossen897 commented 1 month ago

Solved the CAPTCHA problem with manual login. Having some trouble with pyautogui to click on the Sign In button. It can't find the Button from screenshot.

Anyway, thank you very much for helping out.

love for your work @mdmintz