Skip to content

Commit 28ba011

Browse files
Merge pull request #5 from 2captcha/RC-2915-cloudflare-turnstile
RC-2915-cloudflare-turnstile
2 parents cf3948c + 3aff16a commit 28ba011

File tree

3 files changed

+153
-1
lines changed

3 files changed

+153
-1
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Examples of solving captchas using the Python programming language, and the [2captcha-python] and [Selenium] libraries.
44

5-
This repository contains examples of automation of solving the most popular types of captcha, such as [reCAPTCHA][recaptcha-v2-demo], [normal captcha][normal-captcha-demo], [hCaptcha][hcaptcha-demo] and others. Selenium library is used for browser automation in the examples. The [2Captcha] service is used for solving captchas, therefore, for the correct work of the examples it is necessary to have an account in the [captcha solving service][2Captcha] service with a positive balance, or you can try to test it using [sandbox] mode to solve captchas manually.
5+
This repository contains examples of automation of solving the most popular types of captcha, such as [reCAPTCHA][recaptcha-v2-demo], [Cloudflare Turnstile][cloudflare-turnstile], [normal captcha][normal-captcha-demo], [hCaptcha][hcaptcha-demo] and others. Selenium library is used for browser automation in the examples. The [2Captcha] service is used for solving captchas, therefore, for the correct work of the examples it is necessary to have an account in the [captcha solving service][2Captcha] service with a positive balance, or you can try to test it using [sandbox] mode to solve captchas manually.
66
Also, for `proxy` examples to work correctly, you need to have your own `proxy` and set it in the example code. The examples of captcha automation solving use captchas located on the [captchas demo pages](https://2captcha.com/demo).
77

88
We have our own proxies that we can offer you. [Buy residential proxies] to avoid restrictions and blocks. [Quick start].
@@ -26,6 +26,8 @@ Official 2Captcha webpage for [selenium captcha solver](https://2captcha.com/p/s
2626
- [hCaptcha examples](#hcaptcha-examples)
2727
- [hCaptcha](#hcaptcha)
2828
- [hCaptcha + proxy](#hcaptcha--proxy)
29+
- [Cloudflare example](#cloudflare-example)
30+
- [Cloudflare Turnstile](#cloudflare-turnstile)
2931
- [Text captcha example](#text-captcha-example)
3032
- [Normal captcha examples](#normal-captcha-examples)
3133
- [Normal captcha (screenshot)](#normal-captcha-screenshot)
@@ -203,6 +205,24 @@ In these example implements bypassing hCaptcha located on the page https://2capt
203205

204206
**Source code:** [`./examples/hCaptcha/hcaptcha_proxy.py`](./examples/hCaptcha/hcaptcha_proxy.py)
205207

208+
### Cloudflare example
209+
210+
Cloudflare is one of the most popular captcha types. Cloudflare has two types. First type is [Cloudflare Turnstile][cloudflare-turnstile] and second type is [Cloudflare challenge][cloudflare-challenge] page.
211+
212+
- [Cloudflare Turnstile][cloudflare-turnstile] - In this case, the captcha is displayed on the target page, the captcha badge is embedded in the target page code. An example of implementation is shown on the page https://2captcha.com/demo/cloudflare-turnstile.
213+
214+
- [Cloudflare challenge][cloudflare-challenge] - In this case, a redirect to the challenge page will occur to pass the captcha, as implemented here https://2captcha.com/demo/cloudflare-turnstile-challenge.
215+
216+
The approach to bypassing these two types is different, so you need to determine which type of Cloudflare you are facing. Read more about bypassing Cloudflare Turnstile type below.
217+
218+
#### Cloudflare Turnstile
219+
220+
Token-based Cloudflare Turnstile solution.
221+
222+
This example demonstrates how to bypass the Cloudflare Turnstile CAPTCHA located on the page https://2captcha.com/demo/cloudflare-turnstile. The Selenium library is used to automate browser actions and retrieve CAPTCHA parameters. To solve this type of Cloudflare CAPTCHA, it is necessary to send parameters such as pageurl and sitekey to the [2Captcha API](https://2captcha.com/2captcha-api#turnstile). After receiving the solution result (token), the script automatically uses the received answer on the page.
223+
224+
**Source code:** [`./examples/cloudflare_turnstile/cloudflare_turnstile.py`](./examples/cloudflare_turnstile/cloudflare_turnstile.py)
225+
206226
### Text captcha example
207227

208228
Text captcha solutions.
@@ -320,6 +340,8 @@ The graphics and trademarks included in this repository are not covered by the M
320340
[recaptcha-v2-callback-demo]: https://2captcha.com/demo/recaptcha-v2-callback
321341
[recaptcha-v2-enterprise-demo]: https://2captcha.com/demo/recaptcha-v2-enterprise
322342
[recaptcha-v3-enterprise-demo]: https://2captcha.com/demo/recaptcha-v3-enterprise
343+
[cloudflare-turnstile]: https://2captcha.com/demo/cloudflare-turnstile
344+
[cloudflare-challenge]: https://2captcha.com/demo/cloudflare-turnstile-challenge
323345
[normal-captcha-demo]: https://2captcha.com/demo/normal
324346
[hcaptcha-demo]: https://2captcha.com/demo/hcaptcha
325347
[2captcha]: https://2captcha.com
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
from selenium import webdriver
2+
from selenium.webdriver.common.by import By
3+
from selenium.webdriver.support.wait import WebDriverWait
4+
from selenium.webdriver.support import expected_conditions as EC
5+
import os
6+
from twocaptcha import TwoCaptcha
7+
8+
# Description:
9+
# In this example, you will learn how to bypass the Cloudflare Turnstile CAPTCHA located on the page https://2captcha.com/demo/cloudflare-turnstile. This demonstration will guide you through the steps of interacting with and overcoming the CAPTCHA using specific techniques
10+
# The value of the `sitekey` parameter is extracted from the page code automaticly.
11+
12+
# CONFIGURATION
13+
14+
url = "https://2captcha.com/demo/cloudflare-turnstile"
15+
apikey = os.getenv('APIKEY_2CAPTCHA')
16+
17+
18+
# LOCATORS
19+
20+
sitekey_locator = "//div[@id='cf-turnstile']"
21+
css_locator_for_input_send_token = 'input[name="cf-turnstile-response"]'
22+
submit_button_captcha_locator = "//button[@type='submit']"
23+
success_message_locator = "//p[contains(@class,'successMessage')]"
24+
25+
26+
# GETTERS
27+
28+
def get_element(locator):
29+
"""Waits for an element to be clickable and returns it"""
30+
return WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.XPATH, locator)))
31+
32+
33+
# ACTIONS
34+
35+
def get_sitekey(locator):
36+
"""
37+
Extracts the sitekey from the specified element.
38+
39+
Args:
40+
locator (str): The XPath locator of the element containing the sitekey.
41+
Returns:
42+
str: The sitekey value.
43+
"""
44+
sitekey_element = get_element(locator)
45+
sitekey = sitekey_element.get_attribute('data-sitekey')
46+
print(f"Sitekey received: {sitekey}")
47+
return sitekey
48+
49+
def solver_captcha(apikey, sitekey, url):
50+
"""
51+
Solves the Claudflare Turnstile using the 2Captcha service.
52+
53+
Args:
54+
apikey (str): The 2Captcha API key.
55+
sitekey (str): The sitekey for the captcha.
56+
url (str): The URL where the captcha is located.
57+
Returns:
58+
str: The solved captcha code.
59+
"""
60+
solver = TwoCaptcha(apikey)
61+
try:
62+
result = solver.turnstile(sitekey=sitekey, url=url)
63+
print(f"Captcha solved")
64+
return result['code']
65+
except Exception as e:
66+
print(f"An error occurred: {e}")
67+
return None
68+
69+
def send_token(css_locator, captcha_token):
70+
"""
71+
Sends the captcha token to the Claudflare Turnstile response field.
72+
73+
Args:
74+
css_locator (str): The CSS locator for the input field.
75+
captcha_token (str): The solved captcha token.
76+
"""
77+
script = f"""
78+
var element = document.querySelector('{css_locator}');
79+
if (element) {{
80+
element.value = "{captcha_token}";
81+
}}
82+
"""
83+
browser.execute_script(script)
84+
print("Token sent")
85+
86+
def click_check_button(locator):
87+
"""
88+
Clicks the captcha check button.
89+
90+
Args:
91+
locator (str): The XPath locator of the check button.
92+
"""
93+
get_element(locator).click()
94+
print("Pressed the Check button")
95+
96+
def final_message(locator):
97+
"""
98+
Retrieves and prints the final success message.
99+
100+
Args:
101+
locator (str): The XPath locator of the success message.
102+
"""
103+
message = get_element(locator).text
104+
print(message)
105+
106+
# MAIN LOGIC
107+
108+
with webdriver.Chrome() as browser:
109+
110+
browser.get(url)
111+
print('Started')
112+
113+
sitekey = get_sitekey(sitekey_locator)
114+
115+
token = solver_captcha(apikey, sitekey, url)
116+
117+
if token:
118+
119+
send_token(css_locator_for_input_send_token, token)
120+
121+
click_check_button(submit_button_captcha_locator)
122+
123+
final_message(success_message_locator)
124+
125+
print("Finished")
126+
else:
127+
print("Failed to solve captcha")
128+
129+

examples/mtcaptcha/mtcaptcha.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def send_token(css_locator, captcha_token):
6969
Sends the captcha token to the MTCaptcha response field.
7070
7171
Args:
72+
css_locator (str): The CSS locator for the input field.
7273
captcha_token (str): The solved captcha token.
7374
"""
7475
script = f"""

0 commit comments

Comments
 (0)