Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

cloudflare challenge #3933

Answered by mdmintz
naweed1992 asked this question in Q&A
Discussion options

Hi, I have passed cloudflare challenge using seleniumbase, but somehow from yesterday everything broke down, my code does not work anymore. I share my code, I will be pleased if you help me to get through this problem. I have to mention that I am running this code on Ubuntu server.

import asyncio
import json
import os
from typing import Literal
from loguru import logger
from seleniumbase import SB as seleniumBase
logger.add("logs/log-of-{time:YYYY-MM-DD}.log", rotation="4 week")
logger.debug("That's it, Chiron Captcha is simple Like it!")
async def solve_cloudflare_captcha(
 cookies: list[dict], proxy: str, url: str = "https://x.com/"
) -> dict[Literal["cookies", "user_agent", "html_content"], str | dict]:
 proxy = proxy.removeprefix("http://").removeprefix("https://").removesuffix("/")
 logger.info(f"Launching browser session with proxy {proxy!r}")
 # Detect if DISPLAY is available, otherwise enable xvfb
 use_xvfb = False
 if "DISPLAY" not in os.environ or not os.environ["DISPLAY"]:
 logger.warning("No DISPLAY found -> enabling Xvfb virtual display")
 use_xvfb = True
 with seleniumBase(
 uc=True,
 headed=True, # visible window (or virtual one if xvfb=True)
 headless=False, # must be False for GUI automation
 test=True,
 locale="en",
 xvfb=use_xvfb, # auto-enable xvfb if DISPLAY missing
 proxy=proxy,
 ) as sb:
 logger.info(f"Opening URL: {url}")
 sb.open(url)
 logger.info("Adding cookies to session...")
 sb.add_cookies(cookies)
 logger.info("Refreshing the page to apply cookies...")
 sb.refresh()
 html = sb.get_page_source()
 logger.info(f"HTML content on homepage {html[:200]}")
 logger.info("Attempting CAPTCHA interaction...")
 sb.uc_gui_click_captcha() # GUI click for Cloudflare checkbox
 sb.wait_for_ready_state_complete()
 unlock_account_button = 'input[type="submit"]'
 first_page_element = "ul.TextGroup-list"
 if sb.is_element_present(unlock_account_button) and not sb.is_element_visible(
 first_page_element
 ):
 sb.click(unlock_account_button)
 logger.info("Submit button found and clicked")
 else:
 logger.error("Submit button not found")
 logger.info("Extracting updated cookies from browser...")
 cookies = sb.driver.execute_cdp_cmd("Network.getAllCookies", {})["cookies"]
 logger.info("Retrieving user-agent string...")
 user_agent = sb.execute_script("return navigator.userAgent;")
 logger.info("Grabbing page HTML content...")
 html = sb.get_page_source()
 logger.info(f"HTML content on page after solving {html[:200]}")
 if "Just a moment..." in html or sb.get_page_title() == "Just a moment...":
 logger.warning("Cloudflare challenge can't be solved")
 return False
 result = {
 "cookies": json.dumps(cookies),
 "user_agent": user_agent,
 "html_content": html,
 }
 logger.success("Cloudflare challenge solved successfully")
 return result
# === Run the script ===
if __name__ == "__main__":
 # === Proxy Configuration ===
 proxy_url = ""
 # === Cookies for Twitter Session ===
 cleaned_cookies = []
 session_data = asyncio.run(
 solve_cloudflare_captcha(cookies=cleaned_cookies, proxy=proxy_url)
 )
 if session_data:
 logger.info("Session output:")
 logger.info(f"User Agent: {session_data['user_agent']}")
 logger.info(f"Cookies Count: {len(session_data['cookies'])}")
 logger.info(f"HTML Length: {len(session_data['html_content'])}")
 else:
 logger.error("solving failed")
You must be logged in to vote

It looks like you're calling WebDriver methods from CDP Mode, which either doesn't work, or is detectable.

sb.add_cookies(cookies) calls the WebDriver method add_cookie on the backend:

https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/fixtures/base_case.py#L4786

And sb.driver.execute_cdp_cmd is a WebDriver method.

There are CDP Methods for cookies. Here's an example that uses that: examples/cdp_mode/raw_cookies_async.py.

CDP Mode methods are listed at the bottom of SeleniumBase/examples/cdp_mode/ReadMe.md.


As for running in Ubuntu, there have been no issues bypassing Cloudflare Turnstiles.

https://github.com/mdmintz/undetected-testing/actions/runs/17040447895/job/4830...

Replies: 2 comments 2 replies

Comment options

I am also facing the same problem and unfortunately the problem was not solved on my system today and I clicked in the wrong place which causes the rest of the steps to fail due to not passing the captcha.

You must be logged in to vote
2 replies
Comment options

are you working on windows or ubuntu?

Comment options

I use both operating systems, Apple MacOS and Windows, and there are problems on almost all of them!!

Comment options

It looks like you're calling WebDriver methods from CDP Mode, which either doesn't work, or is detectable.

sb.add_cookies(cookies) calls the WebDriver method add_cookie on the backend:

https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/fixtures/base_case.py#L4786

And sb.driver.execute_cdp_cmd is a WebDriver method.

There are CDP Methods for cookies. Here's an example that uses that: examples/cdp_mode/raw_cookies_async.py.

CDP Mode methods are listed at the bottom of SeleniumBase/examples/cdp_mode/ReadMe.md.


As for running in Ubuntu, there have been no issues bypassing Cloudflare Turnstiles.

https://github.com/mdmintz/undetected-testing/actions/runs/17040447895/job/48302665896

You must be logged in to vote
0 replies
Answer selected by mdmintz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /