|
4 | 4 | import time |
5 | 5 |
|
6 | 6 | from captcha_solver_api.tasks import TurnstileTaskProxyless |
7 | | -from common import create_client, show_success |
| 7 | +from common import create_client, required_env, show_success |
8 | 8 | from selenium.common.exceptions import TimeoutException |
9 | 9 | from selenium.webdriver.support.ui import WebDriverWait |
10 | 10 | from seleniumbase import Driver |
11 | 11 |
|
12 | | -URL = os.getenv("TARGET_URL") or "https://2captcha.com/demo/cloudflare-turnstile-challenge" |
| 12 | +URL = required_env("TARGET_URL") |
| 13 | +BROWSER_USER_AGENT = os.getenv("BROWSER_USER_AGENT") or ( |
| 14 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " |
| 15 | + "(KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36" |
| 16 | +) |
13 | 17 |
|
14 | 18 | INTERCEPT_SCRIPT = """ |
15 | 19 | window.__turnstileParams = null; |
16 | 20 | window.__turnstileCallback = null; |
17 | 21 | const timer = setInterval(() => { |
18 | 22 | if (!window.turnstile) return; |
19 | 23 | clearInterval(timer); |
20 | | - window.turnstile.render = (_container, options) => { |
| 24 | + const originalRender = window.turnstile.render; |
| 25 | + window.turnstile.render = (container, options) => { |
21 | 26 | window.__turnstileParams = { |
22 | 27 | websiteKey: options.sitekey, |
23 | 28 | websiteURL: window.location.href, |
|
27 | 32 | userAgent: navigator.userAgent, |
28 | 33 | }; |
29 | 34 | window.__turnstileCallback = options.callback; |
30 | | - return undefined; |
| 35 | + return originalRender ? originalRender(container, options) : undefined; |
31 | 36 | }; |
32 | 37 | }, 10); |
33 | 38 | """ |
@@ -61,24 +66,17 @@ def capture_params(driver): |
61 | 66 | return state["params"] |
62 | 67 |
|
63 | 68 |
|
64 | | -def normalized_user_agent() -> str: |
65 | | - """Read Chrome's UA before launching the challenge browser.""" |
66 | | - with Driver(browser="chrome", headless=True) as driver: |
67 | | - user_agent = driver.execute_script("return navigator.userAgent") |
68 | | - return user_agent.replace("Headless", "").replace("Chromium", "Chrome") |
69 | | - |
70 | | - |
71 | 69 | def main() -> None: |
72 | | - user_agent = normalized_user_agent() |
73 | 70 | with ( |
74 | | - Driver(browser="chrome", headless=False, uc=True, agent=user_agent) as driver, |
| 71 | + Driver(browser="chrome", headless=False, agent=BROWSER_USER_AGENT) as driver, |
75 | 72 | create_client() as client, |
76 | 73 | ): |
| 74 | + driver.get(URL) |
77 | 75 | driver.execute_cdp_cmd( |
78 | 76 | "Page.addScriptToEvaluateOnNewDocument", |
79 | 77 | {"source": INTERCEPT_SCRIPT}, |
80 | 78 | ) |
81 | | - driver.get(URL) |
| 79 | + driver.refresh() |
82 | 80 | params = capture_params(driver) |
83 | 81 | if params is None: |
84 | 82 | return |
|
0 commit comments