A CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) is a system designed to prevent automated bots from accessing certain web pages. Common types include image selections, text recognition, and checkbox-based CAPTCHAs.
CAPTCHAs block scraping bots, causing Selenium scripts to stall or fail. Overcoming them is essential for uninterrupted scraping.
Broadly, there are two ways to bypass CAPTCHA with Selenium: do your best to avoid triggering them in the first place, and using a dedicated Selenium CAPTCHA solver to, well, solve the CAPTCHA, when possible.
Since it’s always better to be proactive than simply reactive, let’s start with the first.
Most CAPTCHAs are triggered in response to suspicious activity. Too many frequent requests from a single IP address is often the key culprit here. You can resolve this by rotating proxies to avoid IP bans or triggering anti-bot measures in the first place.
Secondly, you can ensure your using Selenium with a User-Agent to help better mimic human behavior - or to put it more accurately, the tools and browsers that actual humans use.
Finally, this can be supported by implementing random delays and other browser actions. For websites that analyze behavior and look for the telltale signs of machine perfection, this can help bypass the detection measures that would likely trigger the CAPTCHA.
You can do this quite easily in Selenium:
import random
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
# Initialize WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://example.com')
# Function to introduce random delays
def human_like_delay(min_delay=1, max_delay=3):
time.sleep(random.uniform(min_delay, max_delay))
# Scroll page like a human
scroll_height = driver.execute_script("return document.body.scrollHeight")
for i in range(0, scroll_height, 200):
driver.execute_script(f"window.scrollTo(0, {i});")
human_like_delay(0.5, 1.5)
# Move mouse to an element
element = driver.find_element(By.LINK_TEXT, 'About Us')
actions = ActionChains(driver)
actions.move_to_element(element).pause(random.uniform(0.5, 1.5)).click().perform()
Sometimes you can try everything but still face a CAPTCHA. It’s going to happen so let’s move on to the obvious question: can we automate CAPTCHA using selenium?
Selenium doesn’t solve CAPTCHAs right out of the box. However, there are a number of CAPTCHA-solving services that can be integrated with Selenium. The most common are 2Captcha, AntiCaptcha and DeathByCaptcha, but these are far from the only options available.
So, how can we handle captcha with selenium automation? In this example, we’ll use 2Captcha, as it’s one of the most popular and recommended:
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
# Initialize WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://example.com/captcha-page')
# Extract site key
element = driver.find_element(By.CLASS_NAME, 'g-recaptcha')
site_key = element.get_attribute('data-sitekey')
# Send to 2Captcha
api_key = 'YOUR_2CAPTCHA_API_KEY'
response = requests.get(f'http://2captcha.com/in.php?key={api_key}&method=userrecaptcha&googlekey={site_key}&pageurl=https://example.com')
id = response.text.split('|')[1]
# Poll for solution
time.sleep(20)
result = requests.get(f'http://2captcha.com/res.php?key={api_key}&action=get&id={id}').text
solution = result.split('|')[1]
# Inject solution
script = f"document.getElementById('g-recaptcha-response').innerHTML='{solution}';"
driver.execute_script(script)
driver.find_element(By.ID, 'submit-button').click()
Our community is here to support your growth, so why wait? Join now and let’s build together!