Repository navigation
Expand file tree
/
Copy pathrecaptcha_v2_proxy.py
More file actions
56 lines (49 loc) · 1.69 KB
/
Copy pathrecaptcha_v2_proxy.py
File metadata and controls
56 lines (49 loc) · 1.69 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
"""Solve reCAPTCHA v2 with the same proxy in the browser and API task."""
import time
from captcha_solver_api.tasks import RecaptchaV2Task
from common import (
create_client,
fill_response_field,
logged_example,
proxy_settings,
required_env,
selenium_proxy,
show_success,
wait_for_xpath,
)
from selenium.common.exceptions import TimeoutException
from seleniumbase import Driver
URL = required_env("TARGET_URL")
SITEKEY_LOCATOR = "//div[@id='g-recaptcha']"
SUBMIT_LOCATOR = "//button[@data-action='demo_action']"
@logged_example
def main() -> None:
with (
Driver(browser="chrome", headless=False, cft=True, proxy=selenium_proxy()) as driver,
create_client() as client,
):
driver.get(URL)
try:
sitekey = wait_for_xpath(driver, SITEKEY_LOCATOR).get_attribute("data-sitekey")
except TimeoutException as exc:
raise RuntimeError(
"The target page did not load through the configured proxy. "
"Check the proxy address, credentials, and availability."
) from exc
print(f"Sitekey received: {sitekey}")
solution = client.solve(
RecaptchaV2Task(
websiteURL=URL,
websiteKey=sitekey,
userAgent=driver.execute_script("return navigator.userAgent"),
**proxy_settings(),
)
)
fill_response_field(driver, "g-recaptcha-response", solution["gRecaptchaResponse"])
print("Token sent")
wait_for_xpath(driver, SUBMIT_LOCATOR).click()
print("Pressed the Check button")
show_success(driver)
time.sleep(5)
if __name__ == "__main__":
main()