How to Bypass CAPTCHA with Selenium for Smooth Automation

SwiftProxy
By - Emily Chan
2025-05-19 14:59:38

How to Bypass CAPTCHA with Selenium for Smooth Automation

CAPTCHAs stop over 90% of bots dead in their tracks. And for good reason — they're built to separate humans from machines. But what if your automation is legit? What if your Selenium scripts keep hitting this brick wall and get stuck? Frustrating, right?

You can bypass CAPTCHA automatically. Not by hacking it, but by integrating smart tools that handle the heavy lifting for you. This guide walks you through exactly how to do it. Step by step.

What You Need Before Diving In

Don't start without these essentials:

Python & Selenium installed
Run this if you haven't yet:

pip install --upgrade selenium

A CAPTCHA-solving service
Services like 2Captcha or Anti-Captcha are your best friends here. Sign up, grab an API key, and get ready to automate CAPTCHA solving.

Proxies
Rotating proxies help avoid triggering CAPTCHAs frequently. Proxy Services offer some plans to get you started. Using proxies lowers costs by reducing CAPTCHA requests.

Supporting libraries
To connect with 2Captcha easily, install their Python client:

pip install 2captcha-python

How to Bypass CAPTCHA with Selenium

Here's how to plug everything together and get your script flying over those CAPTCHAs.

Step 1: Import Your Tools

Bring in Selenium, 2Captcha client, and time for pacing:

from selenium.webdriver.common.by import By  
from twocaptcha import TwoCaptcha  
from selenium import webdriver  
import time  

Step 2: Launch Browser and Go to CAPTCHA Page

Make sure you have the correct WebDriver for your browser. Then:

driver = webdriver.Chrome()  # ChromeDriver must be installed and in PATH  
url = "https://2captcha.com/demo/normal"  # Testing page with CAPTCHA  
driver.get(url)  

Step 3: Grab CAPTCHA Image and Solve It

Find the CAPTCHA image URL, send it to 2Captcha, and get the answer:

img = driver.find_element(By.XPATH, "//img[contains(@class,'_2hXzbgz7SSP0DXCyvKWcha')]")  
solver = TwoCaptcha('Your_2Captcha_API_key')  
result = solver.normal(img.get_attribute("src"))  
print("CAPTCHA solved:", result)  

Step 4: Input the Solution and Submit

Paste the solved code into the form and submit it:

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  

Step 5: Verify Success

Check the page for a success message:

message = driver.find_element(By.XPATH, "//p[contains(@class,'_2WOJoV7Dg493S8DW_GobSK')]")  
print("Result:", message.text)  # Should confirm success!  

Troubleshooting Common Roadblocks

Problem: Headless Mode Gets You Flagged

Many sites detect headless browsers and block them — even with custom user-agents.

Fix: Use tools like undetected-chromedriver which hide Selenium's footprints better.
Here's a quick setup:

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 on Navigation

Sometimes the custom user-agent applies only once, causing detection later.

Fix: Set your user-agent consistently when initializing the driver:

options = webdriver.ChromeOptions()  
options.add_argument('user-agent=your_custom_user_agent')  
driver = webdriver.Chrome(options=options)  

Problem: Headless Mode Fails to Launch Correctly

Your browser crashes or fails silently with headless + user-agent options.

Fix: Explicitly set 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)  

Final Thoughts

Automating CAPTCHA solving isn't magic — it's smart integration. Combine Selenium with reliable CAPTCHA-solving services, back it with proxy rotation, and stealthy browser settings, and you'll break through barriers that stop most bots cold.

Note sur l'auteur

SwiftProxy
Emily Chan
Rédactrice en chef chez Swiftproxy
Emily Chan est la rédactrice en chef chez Swiftproxy, avec plus de dix ans d'expérience dans la technologie, les infrastructures numériques et la communication stratégique. Basée à Hong Kong, elle combine une connaissance régionale approfondie avec une voix claire et pratique pour aider les entreprises à naviguer dans le monde en évolution des solutions proxy et de la croissance basée sur les données.
Le contenu fourni sur le blog Swiftproxy est destiné uniquement à des fins d'information et est présenté sans aucune garantie. Swiftproxy ne garantit pas l'exactitude, l'exhaustivité ou la conformité légale des informations contenues, ni n'assume de responsabilité pour le contenu des sites tiers référencés dans le blog. Avant d'engager toute activité de scraping web ou de collecte automatisée de données, il est fortement conseillé aux lecteurs de consulter un conseiller juridique qualifié et de revoir les conditions d'utilisation applicables du site cible. Dans certains cas, une autorisation explicite ou un permis de scraping peut être requis.
Join SwiftProxy Discord community Chat with SwiftProxy support via WhatsApp Chat with SwiftProxy support via Telegram
Chat with SwiftProxy support via Email