DEV Community

How to Easily Bypass CAPTCHA with Selenium and 2Captcha

CAPTCHAs are designed to stop bots in their tracks. They ask users to pick images, solve puzzles, or type scrambled text. Easy for humans. A nightmare for scripts. If you’re using Selenium for automation, CAPTCHAs can grind your workflow to a halt.
But what if you could automatically solve those CAPTCHAs? What if your Selenium script never got stuck again?
Let us show you exactly how to do it — no guesswork, just practical steps.

Requirements Before Getting Started

Make sure your toolkit is ready:

Selenium 4+ and Python installed
Run:

 pip install --upgrade selenium 
Enter fullscreen mode Exit fullscreen mode

CAPTCHA-solving service account
Services like 2Captcha or Anti-Captcha work well. Sign up and grab your API key.
Proxies
Use rotating proxies from providers. They lower how often you face CAPTCHAs — saving you money on solving fees.
Extra Python library for 2Captcha

 pip install 2captcha-python 
Enter fullscreen mode Exit fullscreen mode

How to Use Selenium and 2Captcha to Bypass CAPTCHA

Here’s the no-nonsense way to get past CAPTCHA barriers.

Step 1: Import Your Tools

from selenium.webdriver.common.by import By from twocaptcha import TwoCaptcha from selenium import webdriver import time 
Enter fullscreen mode Exit fullscreen mode

Step 2: Open Chrome and Visit the CAPTCHA Page

driver = webdriver.Chrome() # ChromeDriver must be installed url = "https://2captcha.com/demo/normal" # Sample CAPTCHA for testing driver.get(url) 
Enter fullscreen mode Exit fullscreen mode

Step 3: Locate the CAPTCHA Image and Submit It to 2Captcha

img = driver.find_element(By.XPATH, "//img[contains(@class,'_2hXzbgz7SSP0DXCyvKWcha')]") solver = TwoCaptcha('Your_2Captcha_API_key') # Replace with your key result = solver.normal(img.get_attribute("src")) print("CAPTCHA solved:", result) 
Enter fullscreen mode Exit fullscreen mode

Step 4: Enter the CAPTCHA Solution and Submit

captcha_input = driver.find_element(By.XPATH, "//input[contains(@class,'_26Pq0m_qFk19UXx1w0U5Kv')]") captcha_input.send_keys(result["code"]) submit_btn = driver.find_element(By.XPATH, "//button[contains(@class, 'l2z7-tVRGe-3sq5kU4uu5')]") submit_btn.click() time.sleep(10) # Wait for response 
Enter fullscreen mode Exit fullscreen mode

Step 5: Check If It Worked

message = driver.find_element(By.XPATH, "//p[contains(@class,'_2WOJoV7Dg493S8DW_GobSK')]") print("Result:", message.text) # Expect: CAPTCHA is passed successfully! 
Enter fullscreen mode Exit fullscreen mode

How to Resolve Common Errors

Headless Mode Gets Blocked?
Sites often flag headless browsers instantly.
Solution: Use undetected-chromedriver which better masks headless mode.

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) 
Enter fullscreen mode Exit fullscreen mode

User-Agent Resets After First Request?
If your custom user-agent changes back on page reload, sites can spot you.
Solution: Set it consistently at startup.

options = webdriver.ChromeOptions() options.add_argument('user-agent=your_custom_user_agent') driver = webdriver.Chrome(options=options) 
Enter fullscreen mode Exit fullscreen mode

Headless Chrome Won’t Launch Properly?
Sometimes headless Chrome refuses to start with certain flags.
Solution: 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') options.add_argument('--disable-gpu') driver = webdriver.Chrome(options=options) 
Enter fullscreen mode Exit fullscreen mode

Wrapping Up

CAPTCHA is tough. But with the right tools and approach, you can automate around it — or even through it. Selenium combined with a service like 2Captcha and smart proxy management gives you full control. Handle the common pitfalls. Keep your user-agent and browser stealthy. And watch your automation run smoother than ever.

Top comments (0)