-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtcaptcha.py
129 lines (93 loc) · 3.1 KB
/
mtcaptcha.py
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
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os
import time
from twocaptcha import TwoCaptcha
# CONFIGURATION
url = "https://2captcha.com/demo/mtcaptcha"
apikey = os.getenv('APIKEY_2CAPTCHA')
# LOCATORS
css_locator_for_input_send_token = 'input[name="mtcaptcha-verifiedtoken"]'
submit_button_captcha_locator = "//button[@data-action='demo_action']"
success_message_locator = "//p[contains(@class,'successMessage')]"
# GETTERS
def get_element(locator):
"""Waits for an element to be clickable and returns it"""
return WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.XPATH, locator)))
# ACTIONS
def get_sitekey():
"""
Retrieves the MTCaptcha sitekey from the webpage using JavaScript.
Returns:
str: The sitekey for MTCaptcha.
"""
time.sleep(3) # Adding a delay to ensure the sitekey is loaded
sitekey = browser.execute_script("""
return window.mtcaptchaConfig.sitekey || window.mtcaptcha.getConfiguration().sitekey;
""")
print("Sitekey received")
return sitekey
def solver_captcha(apikey, sitekey, url):
"""
Solves the MTCaptcha 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.mtcaptcha(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(css_locator, captcha_token):
"""
Sends the captcha token to the MTCaptcha response field.
Args:
css_locator (str): The CSS locator for the input field.
captcha_token (str): The solved captcha token.
"""
script = f"""
var element = document.querySelector('{css_locator}');
if (element) {{
element.value = "{captcha_token}";
}}
"""
browser.execute_script(script)
print("Token sent")
def click_check_button(locator):
"""
Clicks the captcha check button.
Args:
locator (str): The XPath locator of the check button.
"""
get_element(locator).click()
print("Pressed the Check button")
def final_message(locator):
"""
Retrieves and prints the final success message.
Args:
locator (str): The XPath locator of the success message.
"""
message = get_element(locator).text
print(message)
# MAIN LOGIC
with webdriver.Chrome() as browser:
browser.get(url)
print("Started")
sitekey = get_sitekey()
token = solver_captcha(apikey, sitekey, url)
if token:
send_token(css_locator_for_input_send_token, token)
click_check_button(submit_button_captcha_locator)
final_message(success_message_locator)
print("Finished")
else:
print("Failed to solve captcha")