2captcha + reCAPTCHA v2 invisible + Selenium

443 Views Asked by At

2captcha has a Python solution for integrating captcha bypass in Selenium. https://2captcha.com/blog/captcha-bypass-in-selenium

This solution works great for reCAPTCHA v2 checkbox.

However, I needed a solution for solving reCAPTCHA v2 invisible.

I slightly modified the code, changed the browser from Chrome to Firefox, and made a working solution. Unfortunately, it only works on the demo page https://recaptcha-demo.appspot.com/recaptcha-v2-invisible.php

from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from twocaptcha import TwoCaptcha
from selenium import webdriver

def create_selenium_driver():
    profile_path = '/home/w4/.mozilla/firefox/zve8sczd.Off/'

    firefox_options = Options()
    firefox_options.profile = profile_path
    #firefox_options.add_argument("--headless")
    firefox_options.add_argument("--disable-gpu")
    firefox_options.add_argument("--no-sandbox")

    driver = webdriver.Firefox(options=firefox_options)
    return driver

# Instantiate the WebDriver using the function
driver = create_selenium_driver()

# Load the target page
captcha_page_url = "https://recaptcha-demo.appspot.com/recaptcha-v2-invisible.php"
driver.get(captcha_page_url)

# Solve the Captcha
print("Solving Captcha")
solver = TwoCaptcha("2313e8866b81a165896f727d083442d7")
response = solver.recaptcha(sitekey='6LcmDCcUAAAAAL5QmnMvDFnfPTP4iCUYRk2MwC0-', url=captcha_page_url)
code = response['code']
print(f"Successfully solved the Captcha. The solve code is {code}")

# Set the solved Captcha
recaptcha_response_element = driver.find_element(By.ID, 'g-recaptcha-response')
driver.execute_script(f'arguments[0].value = "{code}";', recaptcha_response_element)
driver.execute_script('document.forms[0].submit()')


# Pause the execution so you can see the screen after submission before closing the driver
input("Press enter to continue")
driver.close()

But in a real project, for example, when opening the link https://www.aviasales.ru/search/NYC2810LON1 (the page is dynamic), after solving the captcha, I am redirected to the page https://www.aviasales.ru/search and the captcha becomes unsolved again.

The code itself that doesn't work looks like this:

from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from twocaptcha import TwoCaptcha
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def create_selenium_driver():
    profile_path = '/home/w4/.mozilla/firefox/zve8sczd.Off/'

    firefox_options = Options()
    firefox_options.profile = profile_path
    #firefox_options.add_argument("--headless")
    firefox_options.add_argument("--disable-gpu")
    firefox_options.add_argument("--no-sandbox")

    driver = webdriver.Firefox(options=firefox_options)
    return driver

# Instantiate the WebDriver using the function
driver = create_selenium_driver()

# Load the target page
captcha_page_url = "https://www.aviasales.ru/search/NYC2802LON1"
driver.get(captcha_page_url)

# Solve the Captcha
print("Solving Captcha")
solver = TwoCaptcha("2313e8866b81a165896f727d083442d7")
response = solver.recaptcha(sitekey='6LfYMxMcAAAAAEDoxfaGzEZqAeGqV1KXPAV25QX-', url=captcha_page_url)
code = response['code']
print(f"Successfully solved the Captcha. The solve code is {code}")

# Set the solved Captcha
recaptcha_response_element = driver.find_element(By.ID, 'g-recaptcha-response')
driver.execute_script(f'arguments[0].value = "{code}";', recaptcha_response_element)
driver.execute_script('document.forms[0].submit()')

# Pause the execution so you can see the screen after submission before closing the driver
input("Press enter to continue")
driver.close()

If you know how to help me, then I do not mind that you use my API Key 2captcha from the code above for tests.

0

There are 0 best solutions below