使用Selenium+2Captcha自动化注册时reCAPTCHA验证失效求助
问题:Selenium自动化注册时reCAPTCHA验证后按钮仍禁用
使用Selenium+undetected-chromedriver自动化CourseHero注册流程,通过2Captcha/azcaptcha获取reCAPTCHA解决方案后,手动设置g-recaptcha-response内容,但"Create my account"按钮始终处于禁用状态,必须手动完成验证才能激活按钮。
当前代码如下:
import time import undetected_chromedriver as uc from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import random import string import requests register_url = 'https://www.coursehero.com/register' api_key = 'APIKEY' # Replace with your 2Captcha API key success_combinations = [] # Generate a random email def generate_random_email(): random_string = ''.join(random.choices(string.ascii_lowercase, k=5)) email = f"{random_string}@gmail.com" return email # Generate a random password def generate_random_password(): random_password = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) return random_password # Number of times to loop num_iterations = 10 # Configure Selenium options options = uc.ChromeOptions() options.add_argument('--user-data-dir=C:/Users/willi/Downloads/coursehero/chrome') options.add_argument('--profile-directory=ch') options.add_argument("--start-maximized") #options.add_argument(f'--proxy-server={proxy_url}') #options.add_argument(f'--proxy-auth={proxy_username}:{proxy_password}') # options.add_argument('--headless') # Run Chrome in headless mode # Create a new Chrome driver driver = uc.Chrome(options=options) # Perform registration and handle redirection for _ in range(num_iterations): email = generate_random_email() password = generate_random_password() # Open the registration page driver.get(register_url) original_tab_handle = driver.current_window_handle driver.switch_to.window(original_tab_handle) WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[text()="Sign up with email"]'))).click() # Wait for the CAPTCHA to load WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe"))) # Get the CAPTCHA solution from 2Captcha captcha_params = { 'key': api_key, 'method': 'userrecaptcha', 'googlekey': '6LeIuD4bAAAAAPcFHlgJrN8t44BjPiFWmns2-Dt3', # Replace with the reCAPTCHA site key 'pageurl': register_url, } captcha_response = requests.get('http://2captcha.com/in.php', params=captcha_params) captcha_id = captcha_response.text.split('|')[1] # Wait for the CAPTCHA to be solved captcha_solution = '' while not captcha_solution: captcha_result = requests.get(f'http://2captcha.com/res.php?key={api_key}&action=get&id={captcha_id}') if 'OK' in captcha_result.text: captcha_solution = captcha_result.text.split('|')[1] print(f"captcha Solved: {captcha_solution}") # Switch back to the default content driver.switch_to.default_content() # Fill in the registration form email_input = driver.find_element(By.ID, 'email') password_input = driver.find_element(By.ID, 'password') email_input.send_keys(email) password_input.send_keys(password) # Set the CAPTCHA solution using JavaScript recaptcha_response_element = driver.find_element(By.CSS_SELECTOR, 'textarea[id="g-recaptcha-response"]') driver.execute_script(f'arguments[0].innerHTML = "{captcha_solution}";', recaptcha_response_element) school_input = driver.find_element(By.ID, 'schoolSelection') school_input.send_keys('Warren High School') # Click on the school option WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[contains(text(), "Warren High School")]'))).click() create_account_button = driver.find_element(By.XPATH, '//button[text()="Create my account"]') time.sleep(5) create_account_button.click() # Wait for redirection time.sleep(2) # Adjust the delay if needed # Check if registration was successful if 'payment' in driver.current_url: # Registration was successful success_combinations.append((email, password)) # Save successful combinations to a text file output_file = 'successful_combinations.txt' with open(output_file, 'w') as file: for email, password in success_combinations: file.write(f"Email: {email}\tPassword: {password}\n") print("Registration completed. Successful combinations saved to", output_file) # Close the browser driver.quit()
解决建议
1. 触发reCAPTCHA回调函数
直接修改g-recaptcha-response的内容后,必须调用Google reCAPTCHA的回调函数,网站才会识别验证完成。默认回调为grecaptcha.submit(),如果网站有自定义回调,可通过页面源码搜索data-callback属性获取:
# 替换原有的设置captcha代码 recaptcha_response_element = driver.find_element(By.CSS_SELECTOR, 'textarea[id="g-recaptcha-response"]') # 先设置元素可见(默认隐藏) driver.execute_script("arguments[0].style.display = 'block';", recaptcha_response_element) # 设置value属性(而非innerHTML) driver.execute_script(f'arguments[0].value = "{captcha_solution}";', recaptcha_response_element) # 触发回调 driver.execute_script("grecaptcha.submit();")
2. 等待按钮状态更新
替换原有的time.sleep(5)+直接点击逻辑,等待按钮变为可点击状态后再操作:
WebDriverWait(driver, 15).until( EC.element_to_be_clickable((By.XPATH, '//button[text()="Create my account"]')) ).click()
3. 校验2Captcha参数
- 确认
googlekey(sitekey)正确:通过页面源码搜索data-sitekey获取最新值,避免sitekey过期 - 确认
pageurl与当前页面URL完全一致(注意是否有重定向后的URL)
4. 降低被检测风险
- 每次循环使用新的Chrome配置文件,避免固定profile被识别
- 添加随机延迟模拟人类操作(比如输入表单后等待1-3秒)
- 更新undetected-chromedriver到最新版本
内容的提问来源于stack exchange,提问作者Samurai6465
相关产品推荐
相关产品推荐

