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
5.03k stars 945 forks source link

Detect cloudflare using its own method of bypassing #2946

Closed leo562 closed 1 month ago

leo562 commented 1 month ago

Hi, I apologize if I use my own method and not the built-in sb.uc_gui_click_captcha() . I have cloudflare detecting SeleniumBase if I use my own image crawling method! What could this be related to? . Code -


import pyautogui
import asyncio
from seleniumbase import SB

screenshot_path = 'click.png'

ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"

async def search_and_click():

    while True:
        try:
            location = pyautogui.locateOnScreen(screenshot_path, confidence=0.8)  
            if location:
                center = pyautogui.center(location)
                print(f"Found at: x={center.x}, y={center.y}")  

                pyautogui.moveTo(center.x, center.y, duration=1)  

                pyautogui.click()

                break  # Остановить цикл после успешного клика
        except pyautogui.ImageNotFoundException:
            pass
        await asyncio.sleep(1)  

def check_title_and_handle_captcha(sb):
    title = sb.driver.title
    print(f"Title: {title}")
    if "Just a moment" in title or "Один момент" in title:
        asyncio.run(search_and_click())  
        time.sleep(10)  
        check_title_and_handle_captcha(sb)  

with SB(uc=True, test=True) as sb:
    url = "https://nopecha.com/demo/cloudflare"
    sb.uc_open_with_reconnect(url, reconnect_time=9)

    time.sleep(3)  
    check_title_and_handle_captcha(sb)  

    sb.post_message("SeleniumBase wasn't detected", duration=3)

    cookies = sb.driver.get_cookies()
    cf_clearance_cookie = next((cookie for cookie in cookies if cookie['name'] == 'cf_clearance'), None)
    if cf_clearance_cookie:
        print(f"Cookie: cf_clearance={cf_clearance_cookie['value']}")
    else:
        print("cf_clearance cookie not found")

here is a png - click .

Thank you very much for your hard work!

mdmintz commented 1 month ago

From the UC Mode Documentation:

👤 What makes UC Mode work?

Here are the 3 primary things that UC Mode does to make bots appear human:


Your method did not disconnect chromedriver from Chrome. That's one of the things that UC Mode methods do to avoid detection.

You can also call sb.uc_open_with_disconnect(url) so that the driver is disconnected in advance.