-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrecaptcha_v2_callback_variant1.py
More file actions
129 lines (99 loc) · 3.83 KB
/
recaptcha_v2_callback_variant1.py
File metadata and controls
129 lines (99 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import time
from seleniumbase import Driver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from twocaptcha import TwoCaptcha
# Description:
# The value of the `sitekey` parameter is extracted from the page code automaticly.
# The value of the callback function “verifyDemoRecaptcha()” is specified manually.
# The name of the “verifyDemoRecaptcha()” function may be different when bypassing captcha on another page.
# You need to fugure out the name of the callback function yourself and specify it in the code.
# CONFIGURATION
url = "https://2captcha.com/demo/recaptcha-v2-callback"
apikey = os.getenv('APIKEY_2CAPTCHA')
# LOCATORS
sitekey_locator = "//div[@id='g-recaptcha']"
success_message_locator = "//p[contains(@class,'successMessage')]"
# GETTERS
def get_element(browser, locator):
"""
Waits for an element to be clickable and returns it.
This helper can be copied and reused in other projects that use SeleniumBase.
"""
return WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.XPATH, locator)))
# ACTIONS
def get_sitekey(browser, locator):
"""
Extracts the sitekey from the specified element.
Args:
browser: The SeleniumBase driver instance.
locator (str): The XPath locator of the element.
Returns:
str: The sitekey value.
"""
sitekey_element = get_element(browser, locator)
sitekey = sitekey_element.get_attribute('data-sitekey')
print(f"Sitekey received: {sitekey}")
return sitekey
def solver_captcha(apikey, sitekey, url):
"""
Solves the reCaptcha using the 2Captcha service.
Args:
apikey (str): The 2Captcha API key.
sitekey (str): The sitekey for the captcha.
url (str): The URL where the captcha is located.
Returns:
str: The solved captcha code.
"""
solver = TwoCaptcha(apikey)
try:
result = solver.recaptcha(sitekey=sitekey, url=url)
print(f"Captcha solved")
return result['code']
except Exception as e:
print(f"An error occurred: {e}")
return None
def send_token(browser, token):
# verifyDemoRecaptcha() it is JavaScript callback function on page with captcha.
# callback function executing for apply token.
script = f"verifyDemoRecaptcha('{token}');"
browser.execute_script(script)
print("The token is sent to the callback function")
def final_message(browser, locator):
"""
Retrieves and prints the final success message.
Args:
locator (str): The XPath locator of the success message.
"""
message = get_element(browser, locator).text
print(message)
def main():
"""
Runs the demo flow for solving reCaptcha v2 with a callback using 2Captcha.
Helper functions (`get_sitekey`, `solver_captcha`, `send_token`, etc.)
are designed so they can be copied and reused independently.
"""
if not apikey:
raise RuntimeError("Set APIKEY_2CAPTCHA environment variable")
with Driver(browser="chrome", headless=False) as browser:
# Go to the specified URL
browser.get(url)
print('Started')
# Getting sitekey from the sitekey element
sitekey = get_sitekey(browser, sitekey_locator)
# Solving the captcha and receiving a token
token = solver_captcha(apikey, sitekey, url)
if token:
# Sending solved captcha token to callback
send_token(browser, token)
# Receiving and displaying a success message
final_message(browser, success_message_locator)
# Explicit pause to observe the result
time.sleep(5)
print("Finished")
else:
print("Failed to solve captcha")
if __name__ == "__main__":
main()