How to bypass captcha using Selenium
#
How to Bypass Captcha Using Selenium
What is Selenium
Selenium WebDriver is a web framework that allows you to automate browser interactions. It enables the creation of solutions for bypassing captchas by controlling the browser in the same way a real user would. Selenium supports multiple programming languages and can operate both locally and on remote machines.
To bypass captchas in Selenium, integration with an automatic recognition service like the 2Captcha API is required. This allows captchas to be solved programmatically, which is particularly useful for tasks such as web scraping, testing, and bot creation.
Selenium also supports headless mode (without a graphical interface), making it convenient for automation. Additionally, there are plugins available to hide the fact of automation, which is crucial for successful data parsing.
How to Bypass Captcha Using Selenium in Python, Java, C#, Ruby, and Other Languages
Using the 2Captcha API and Selenium, you can bypass various types of captchas, including reCAPTCHA. Below is an example of solving reCAPTCHA using Python and Selenium.
Example: Solving reCAPTCHA Using Python and Selenium
Let’s walk through the step-by-step process of bypassing reCAPTCHA on the page https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php:
-
Installing Required Components
The following libraries are required:- 2captcha-python: The official Python SDK for integrating with the 2Captcha API.
- Selenium: For browser control.
- webdriver-manager: For automatic downloading and management of browser drivers.
Install the libraries using the following command:
python -m pip install 2captcha-python selenium webdriver-manager
-
Finding the Site Key
The site key is a unique identifier assigned by Google to each reCAPTCHA form. To solve the captcha, this key must be passed to the API.To find the site key:
- Go to the page https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php.
- Open the developer tools (Ctrl/Cmd + Shift + I).
- Locate the
data-sitekey
attribute and copy its value.
-
Solving the Captcha
Below is an example code for solving reCAPTCHA using Selenium and the 2Captcha API. Replace2CAPTCHA_API_KEY
with your API key andSITE_KEY
with the value obtained in the previous step.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 # Initialize 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 the captcha") solver = TwoCaptcha("2CAPTCHA_API_KEY") response = solver.recaptcha(sitekey='SITE_KEY', url=captcha_page_url) code = response['code'] print(f"Captcha successfully solved. Solution code: {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 to check the result input("Press Enter to continue") driver.close()
In this code:
- WebDriver is initialized to control the browser.
- The target page with reCAPTCHA is loaded.
- The 2Captcha API is used to solve the captcha.
- The solved captcha code is passed into the form, which is then submitted.
-
Running and Verification
After executing the code, the captcha will be solved, and you will see the successful form submission screen.
Solving Captchas Using the 2Captcha API and Selenium in Other Languages
The 2Captcha API supports integration with Selenium in various programming languages. Below are links to documentation and modules for the most popular languages:
-
JavaScript
Selenium Documentation: https://www.selenium.dev/selenium/docs/api/javascript/
2Captcha Module: https://github.com/2captcha/2captcha-javascript -
Python
Selenium Documentation: https://www.selenium.dev/selenium/docs/api/py/api.html
2Captcha Module: https://github.com/2captcha/2captcha-python
Examples: https://github.com/2captcha/captcha-solver-selenium-python-examples -
Java
Selenium Documentation: https://www.selenium.dev/selenium/docs/api/java/index.html
2Captcha Module: https://github.com/2captcha/2captcha-java -
C#
Selenium Documentation: https://www.selenium.dev/selenium/docs/api/dotnet/
2Captcha Module: https://github.com/2captcha/2captcha-csharp -
Ruby
Selenium Documentation: https://www.selenium.dev/selenium/docs/api/rb/
2Captcha Module: https://github.com/2captcha/2captcha-ruby
Conclusion
Integrating the 2Captcha API with Selenium allows for efficient captcha solving, which is particularly useful for automating tasks such as web scraping, testing, and bot creation. Using the provided examples and instructions, you can easily implement captcha-solving solutions in your projects using Python, Java, C#, Ruby, and other languages.
For more information on the capabilities of the 2Captcha API, visit the official documentation.