Skip to content

Commit 0c0639e

Browse files
authored
Create index.py
0 parents  commit 0c0639e

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

index.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from selenium import webdriver
2+
from selenium.webdriver.common.by import By
3+
from selenium.webdriver.support.ui import WebDriverWait
4+
from selenium.webdriver.support import expected_conditions as EC
5+
import time
6+
from capmonster_python import RecaptchaV2EnterpriseTask
7+
8+
API_KEY_CAPMONSTER = ""
9+
url_to_get = ""
10+
def goToUrl(driver):
11+
global url_to_get
12+
driver.get(url_to_get)
13+
14+
def resolver_captcha_capmonster(driver, max_attempts=3):
15+
attempts = 0
16+
global API_KEY_CAPMONSTER
17+
18+
while attempts < max_attempts:
19+
try:
20+
WebDriverWait(driver, 10).until(
21+
EC.presence_of_element_located((By.XPATH, '//*[@id="g-recaptcha-response"]'))
22+
)
23+
24+
solver = RecaptchaV2EnterpriseTask(API_KEY_CAPMONSTER)
25+
26+
grecaptcha_cfg = driver.execute_script("return typeof ___grecaptcha_cfg !== 'undefined'")
27+
if not grecaptcha_cfg:
28+
print("Dont find ___grecaptcha_cfg.")
29+
return
30+
31+
siteApiKey = driver.execute_script("""
32+
for(const object of Object.values(___grecaptcha_cfg.clients[0])){
33+
if(!object || typeof object != 'object')
34+
continue;
35+
const elements = Object.values(object);
36+
if(elements.length == 0 || elements == undefined)
37+
continue;
38+
for(const element of elements){
39+
if(element == null || typeof element != 'object')
40+
continue;
41+
if(Object.keys(element).includes('sitekey')){
42+
return (element['sitekey']);
43+
}
44+
}
45+
}
46+
""")
47+
siteUrl = driver.current_url
48+
49+
task_id = solver.create_task(siteUrl, siteApiKey)
50+
result = solver.join_task_result(task_id)
51+
52+
driver.execute_script("""
53+
for(const object of Object.values(___grecaptcha_cfg.clients[0])){
54+
if(!object || typeof object != 'object')
55+
continue;
56+
const elements = Object.values(object);
57+
if(elements.length == 0 || elements == undefined)
58+
continue;
59+
for(const element of elements){
60+
if(element == null || typeof element != 'object')
61+
continue;
62+
if(Object.keys(element).includes('sitekey')){
63+
element['callback']('""" + result.get('text') + """');
64+
}
65+
}
66+
}
67+
""")
68+
return "OK"
69+
70+
except Exception as e:
71+
attempts += 1
72+
print(f"Error solving CAPTCHA with CapMonster (Attempt {attempts}): {str(e)}")
73+
if attempts < max_attempts:
74+
time.sleep(2)
75+
else:
76+
print("Maximum number of attempts reached. Could not solve the CAPTCHA.")
77+
return
78+
79+
def initScreen():
80+
options = webdriver.ChromeOptions()
81+
options.add_argument("-disable-quic")
82+
options.add_argument('--no-sandbox')
83+
options.add_argument('--disable-dev-shm-usage')
84+
options.add_argument("--disable-blink-features=AutomationControlled")
85+
options.add_argument("--disable-popup-blocking")
86+
options.add_argument("--disable-infobars")
87+
options.add_experimental_option("excludeSwitches", ["enable-automation"])
88+
options.add_experimental_option('useAutomationExtension', False)
89+
options.add_argument(f'user-agent=Mozilla/5.0')
90+
91+
driver = webdriver.Chrome(options=options)
92+
goToUrl(driver)
93+
94+
driver.implicitly_wait(30)
95+
96+
resolver_captcha_capmonster(driver)
97+
driver.quit()
98+
99+
100+
initScreen()

0 commit comments

Comments
 (0)