Bypass CAPTCHA with Selenium Without Getting Blocked
CAPTCHAs block over 80% of automated web scripts. They’re designed to weed out bots — and rightly so. But if you’re automating workflows with Selenium, hitting a CAPTCHA can bring everything to a grinding halt. Imagine your script stuck, waiting for human input. Frustrating, right?
There’s a way around this. Let us show you how to bypass CAPTCHA automatically, keeping your Selenium automation running smooth and uninterrupted.
Things to Prepare Before You Start
Selenium with Python — Make sure you’re running Selenium 4 or later for the best WebDriver support.
pip install selenium
pip install --upgrade selenium
CAPTCHA Solving Service — Services like 2Captcha or Anti-Captcha handle the hard work of cracking CAPTCHA for you. Sign up and grab your API key.
Proxies — Rotate your IP addresses with proxies. This reduces how often CAPTCHAs pop up, saving you money and hassle.
Extra Python Libraries — For 2Captcha integration, install:
pip install 2captcha-python
Using Selenium and 2Captcha to Bypass CAPTCHA
Here’s the core workflow, broken down:
1. Import the Tools
from selenium.webdriver.common.by import By
from twocaptcha import TwoCaptcha
from selenium import webdriver
import time
2. Open the Browser and Navigate
driver = webdriver.Chrome() # Make sure ChromeDriver is installed and in PATH
url = "https://2captcha.com/demo/normal" # A handy test page
driver.get(url)
3. Find and Solve the CAPTCHA
Locate the CAPTCHA image URL and send it to 2Captcha for solving.
imgResults = driver.find_elements(By.XPATH, "//img[contains(@class,'_2hXzbgz7SSP0DXCyvKWcha')]")
solver = TwoCaptcha('Your_2Captcha_API_key') # Replace with your actual key
result = solver.normal(imgResults[0].get_attribute("src"))
print("Solved CAPTCHA: " + str(result))
4. Submit the Solution
Fill in the CAPTCHA field with the solved code and hit submit.
captchafield = driver.find_element(By.XPATH, "//input[contains(@class,'_26Pq0m_qFk19UXx1w0U5Kv')]")
captchafield.send_keys(result["code"])
button = driver.find_element(By.XPATH, "//button[contains(@class, 'l2z7-tVRGe-3sq5kU4uu5')]")
button.click()
time.sleep(10) # Give the page time to respond
5. Check the Outcome
Confirm success by grabbing the confirmation message from the page.
messagefield = driver.find_element(By.XPATH, "//p[contains(@class,'_2WOJoV7Dg493S8DW_GobSK')]")
print("Result: " + messagefield.text) # Should say: Captcha is passed successfully!
Troubleshooting Common Issues
Problem: Headless mode triggers CAPTCHA detection.
Fix: Use undetected-chromedriver with carefully set options.
from undetected_chromedriver.v2 import Chrome, ChromeOptions
options = ChromeOptions()
options.add_argument('--headless')
options.add_argument('user-agent=your_custom_user_agent')
driver = Chrome(options=options)
Problem: User-agent resets after initial page load.
Fix: Set user-agent persistently with ChromeOptions:
options = webdriver.ChromeOptions()
options.add_argument('user-agent=your_custom_user_agent')
driver = webdriver.Chrome(options=options)
driver.get("https://example.com")
Problem: Headless Chrome won’t launch correctly with custom user-agent.
Fix: Add window size and GPU flags:
options = webdriver.ChromeOptions()
options.add_argument('user-agent=your_custom_user_agent')
options.add_argument('--headless')
options.add_argument('--window-size=1920x1080')
driver = webdriver.Chrome(options=options)
Final Thoughts
Bypassing CAPTCHA with Selenium isn’t magic—it’s precision. The key lies in blending smart tooling, savvy proxy use, and stealthy browser settings. Follow these steps. Test thoroughly. Watch your automation move past those CAPTCHA roadblocks like a pro.