Repository navigation
Expand file tree
/
Copy pathrecaptcha_v2.py
More file actions
49 lines (41 loc) · 1.55 KB
/
Copy pathrecaptcha_v2.py
File metadata and controls
49 lines (41 loc) · 1.55 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
"""Solve reCAPTCHA v2 in a SeleniumBase browser session."""
import os
import time
from captcha_solver_api.tasks import RecaptchaV2TaskProxyless
from common import (
create_client,
fill_response_field,
logged_example,
wait_for_css,
wait_for_xpath,
)
from seleniumbase import Driver
URL = os.getenv("TARGET_URL") or "https://www.google.com/recaptcha/api2/demo"
SITEKEY_LOCATOR = "//*[@data-sitekey]"
SUBMIT_LOCATOR = "//*[@type='submit']"
SUCCESS_SELECTOR = ".recaptcha-success, p.successMessage"
@logged_example
def main() -> None:
with Driver(browser="chrome", headless=False) as driver, create_client() as client:
driver.get(URL)
widget = wait_for_xpath(driver, SITEKEY_LOCATOR)
sitekey = widget.get_attribute("data-sitekey")
if not sitekey:
raise RuntimeError("No reCAPTCHA sitekey found")
print(f"Sitekey received: {sitekey}")
solution = client.solve(
RecaptchaV2TaskProxyless(
websiteURL=URL,
websiteKey=sitekey,
userAgent=driver.execute_script("return navigator.userAgent"),
)
)
# Token injection and submission are site-specific; adapt this step to the target page.
fill_response_field(driver, "g-recaptcha-response", solution["gRecaptchaResponse"])
print("Token sent")
wait_for_xpath(driver, SUBMIT_LOCATOR).click()
print("Pressed the Check button")
print(wait_for_css(driver, SUCCESS_SELECTOR).text)
time.sleep(5)
if __name__ == "__main__":
main()