Captcha bypass in Selenium

selenium captcha best automatically solving service

Using the API and Selenium you can create a Selenium captcha solver solution.

Selenium automates browsers. What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing and parsing.

To bypass captcha checks in Selenium, a special recognition automation service is required. There is a manual to learn how to use the captcha bypass API together with the Selenium library to perform web scraping, testing, and creating website bots.

Selenium is a Python library that provides a high-level API to control Chrome or Chromium and Firefox or Geckodriver over the DevTools Protocol. Selenium runs non-headless by default but can be configured to run headless. There are plugins for Selenium that allow you to hide the fact that automation occurs. Hiding the fact of automation is important when developing parsers, as this will allow the browser to look more like a person and not be exposed.

How to solve captcha in Selenium

The following is a step by step explanation of the reCAPTCHA bypass process at https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php:

  • Install the required components
  • Find the site key parameter of the reCAPTCHA challenge on the target webpage
  • Solve the reCAPTCHA using API
  • Submit solved captcha

Installing components

You need to install the following libraries:

  • 2Captcha: Official Python SDK for easy integration with 2Captcha API
  • Selenium
  • webdriver-manager: This library simplifies the download and usage of drivers for Selenium

To install these libraries, run the following command:

python -m pip install 2captcha-python selenium webdriver-manager

Next, you need to find a site key parameter and create a Python file where you write captcha-solving code.

Find site key

The site key is a unique identifier given by Google to all reCAPTCHA an forms, which uniquely identifies the captcha. To solve the captcha, you need to send the site key to the captcha.

To find site key of webpage follow these steps:

sitekey

Solve the сaptcha

selenium solve the captcha

Next, write Selenium code to visit the target page and solve the captcha using 2Captcha.

The following code does just that, remember to replace 2CAPTCHA_API_KEY with your 2Captcha API key and SITE_KEY with the site key you stored earlier.

from selenium.webdriver.common.by import By
from twocaptcha import TwoCaptcha
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver

# Instantiate the WebDriver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

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

# Solve the Captcha
print("Solving Captcha")
solver = TwoCaptcha("2CAPTCHA_API_KEY")
response = solver.recaptcha(sitekey='SITE_KEY', url=captcha_page_url)
code = response['code']
print(f"Successfully solved the Captcha. The solve code is {code}")

In this code, we initialize the TwoCaptcha object with the 2captcha API Key and solve the Captcha by calling the recaptcha method passing in the site key and the current page URL.

The recaptcha method returns a dictionary containing the solved captcha code which is logged to the console.

Note that The captcha solving process may take some time, so please be patient

Submit solved captcha

selenium bypass captcha

Next, we will find the g-recaptcha-response element, set its value to the solved captcha code, and submit the form.

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

# Submit the form
submit_btn = driver.find_element(By.CSS_SELECTOR, 'button[type="submit"]')
submit_btn.click()

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

Final code

The following is the final code of the tutorial to solve captcha using API.

from selenium.webdriver.common.by import By
from twocaptcha import TwoCaptcha
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver

# Instantiate the WebDriver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

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

# Solve the Captcha
print("Solving Captcha")
solver = TwoCaptcha("2CAPTCHA_API_KEY")
response = solver.recaptcha(sitekey='SITE_KEY', 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)

# Submit the form
submit_btn = driver.find_element(By.CSS_SELECTOR, 'button[type="submit"]')
submit_btn.click()

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

Replace 2CAPTCHA_API_KEY, SITE_KEY with their values and run the code, the captcha will be solved and you will see the following success screen:

captcha

Conclusion

In this article we've learnt how to solve captcha with Selenium using 2Captcha. To summarize, the general steps for solving captcha in Selenium are as follows:

  • Create an account on 2Captcha, add funds and note the API key
  • Note the site key of the target captcha.
  • Submit the site key and page URL to 2Captcha to solve.
  • Set the solved captcha code on the appropriate element and submit the form.

It's worth noting that the process of solving other captchas, such Arkose labs, is similar. To learn more about solving different types of captchas, please visit captcha breaker page.

Price

There are various types of captchas available, such as Arkose, reCAPTCHA. The cost of solving a captcha depends on the type being solved.

To find a more detailed cost for solving captchas, please visit pricing page.

You can use multiple programming languages like Java, C#, C++, PHP, Python, and Ruby.