|
| 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 | + |
0 commit comments