-
Notifications
You must be signed in to change notification settings - Fork 0
/
normal_captcha_screenshot_params.py
132 lines (102 loc) · 3.54 KB
/
normal_captcha_screenshot_params.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
127
128
129
130
131
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
from twocaptcha import TwoCaptcha
# CONFIGURATION
url = "https://2captcha.com/demo/normal"
apikey = os.getenv('APIKEY_2CAPTCHA')
# ADVANCED CAPTCHA OPTIONS
extra_options = {
"numeric": 4,
"minLen": 4,
"maxLen": 10,
"lang": "en"
}
# LOCATORS
img_locator = "//img[@class='_captchaImage_rrn3u_9']"
input_captcha_locator = "//input[@id='simple-captcha-field']"
submit_button_captcha_locator = "//button[@type='submit']"
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 solver_captcha(image, apikey, **extra_options):
"""
Solves a captcha using the 2Captcha service and returns the solution code
Args:
image (str): Path to the captcha image
apikey (str): API key to access the 2Captcha service
Returns:
str: Captcha solution code, if successful, otherwise None
"""
solver = TwoCaptcha(apikey)
try:
result = solver.normal(image, **extra_options)
print(f"Captcha solved. Code: {result['code']}")
return result['code']
except Exception as e:
print(f"An error occurred: {e}")
return None
def get_image_base64(locator):
"""
Captures a screenshot of the element specified by the locator and returns it as a base64-encoded string.
Args:
locator (str): The XPath locator of the image element.
Returns:
str: The base64-encoded screenshot of the image element.
"""
image_element = get_element(locator)
base64_image = image_element.screenshot_as_base64
return base64_image
def input_captcha_code(locator, code):
"""
Enters the captcha solution code into the input field on the web page
Args:
locator (str): XPATH locator of the captcha input field
code (str): Captcha solution code
"""
input_field = get_element(locator)
input_field.send_keys(code)
print("Entered the answer to the captcha")
def click_check_button(locator):
"""
Clicks the check button on a web page
Args:
locator (str): XPATH locator of the captcha verification button
"""
button = get_element(locator)
button.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
# Automatically closes the browser after block execution completes
with webdriver.Chrome() as browser:
# Go to page with captcha
browser.get(url)
print("Started")
# Getting captcha image in base64 format
image_base64 = get_image_base64(img_locator)
# Solving captcha using 2Captcha
code = solver_captcha(image_base64, apikey, **extra_options)
if code:
# Entering captcha code
input_captcha_code(input_captcha_locator, code)
# Pressing the test button
click_check_button(submit_button_captcha_locator)
# Receiving and displaying a success message
final_message(success_message_locator)
browser.implicitly_wait(5)
print("Finished")
else:
print("Failed to solve captcha")