-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathsync_recaptcha_selenium.py
More file actions
53 lines (40 loc) · 1.55 KB
/
sync_recaptcha_selenium.py
File metadata and controls
53 lines (40 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
50
51
52
53
import time
from os import environ
from python_anticaptcha import AnticaptchaClient, NoCaptchaTaskProxylessTask
api_key = environ["ANTICAPTCHA_API_KEY"]
invisible_captcha = True
url = f"https://www.google.com/recaptcha/api2/demo?invisible={str(invisible_captcha)}"
EXPECTED_RESULT = "Verification Success... Hooray!"
client = AnticaptchaClient(api_key)
def get_token(url, site_key, invisible):
task = NoCaptchaTaskProxylessTask(
website_url=url, website_key=site_key, is_invisible=invisible
)
job = client.createTask(task)
job.join(maximum_time=60 * 15)
return job.get_solution_response()
def process(driver):
driver.get(url)
site_key = get_sitekey(driver)
print("Found site-key", site_key)
token = get_token(url, site_key, invisible_captcha)
print("Found token", token)
form_submit(driver, token)
return driver.find_element_by_class_name("recaptcha-success").text
def form_submit(driver, token):
driver.execute_script(
f"document.getElementById('g-recaptcha-response').innerHTML='{token}';"
)
driver.execute_script(f"onSuccess('{token}')")
time.sleep(1)
def get_sitekey(driver):
return driver.find_element_by_class_name("g-recaptcha").get_attribute(
"data-sitekey"
)
if __name__ == "__main__":
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option('prefs', {'intl.accept_languages': 'en_US'})
driver = Chrome(options=options)
assert EXPECTED_RESULT in process(driver)