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.

關於作者

SwiftProxy
Emily Chan
Swiftproxy首席撰稿人
Emily Chan是Swiftproxy的首席撰稿人,擁有十多年技術、數字基礎設施和戰略傳播的經驗。她常駐香港,結合區域洞察力和清晰實用的表達,幫助企業駕馭不斷變化的代理IP解決方案和數據驅動增長。
Swiftproxy部落格提供的內容僅供參考,不提供任何形式的保證。Swiftproxy不保證所含資訊的準確性、完整性或合法合規性,也不對部落格中引用的第三方網站內容承擔任何責任。讀者在進行任何網頁抓取或自動化資料蒐集活動之前,強烈建議諮詢合格的法律顧問,並仔細閱讀目標網站的服務條款。在某些情況下,可能需要明確授權或抓取許可。
Join SwiftProxy Discord community Chat with SwiftProxy support via WhatsApp Chat with SwiftProxy support via Telegram
Chat with SwiftProxy support via Email