pythonselenium-webdriverweb-scrapingrecaptchaundetected-chromedriver

trying to automate registration but i cant seem to solve recaptcha selenium


I'm currently trying to automate the registration / login process on a website, but both login and sign up have reCAPTCHA. I'm using 2captcha to solve the captcha but I don't think it is applying the solution correctly as the create my account button stays disabled until I manually solve it.

this is what I have so far:

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()

    # Execute the callback function to submit the form


    # 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))

    # Fill out the school field
    

# 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()

I'm really just stuck here, I've even tried using both azcaptcha and 2captcha extensions to try and solve them but no success. Any suggestions are appreciated!


Solution

  • I ended up solving this problem solving audio captcha. You are able to download the mp3 file for your captcha and then you can use something like openAIs speech to text api to get your solution and have selenium type it on text box. Please not if your ip score is bad you wont be allowed to get audio captchas(same thing if you spam requests) so if you are planning on solving alot of captchas this way you will need HQ proxies.