How to Bypass CAPTCHAs With Selenium in Python

Learn how to bypass CAPTCHAs in Selenium with this comprehensive tutorial. Discover practical techniques to handle CAPTCHA challenges in web scraping.
10 min read
How to bypass CAPTCHAs with Selenium blog image

In this Selenium CAPTCHA bypass tutorial, you will learn:

  • What CAPTCHAs are and whether they can be avoided
  • How to avoid a CAPTCHA in Selenium
  • What to do if a CAPTCHA still appears

Let’s dive in!

What Are CAPTCHAs and Is It Possible To Avoid Them?

A CAPTCHA, short for “Completely Automated Public Turing test to tell Computers and Humans Apart,” is a mechanism designed to differentiate between human users and bots. It presents challenges that are easy for humans but difficult for machines to solve. Well-known CAPTCHA providers include Google reCAPTCHA, hCaptcha, and BotDetect.

Popular types of CAPTCHA include:

  • Text-based challenges: Users need to type a sequence of distorted letters and numbers.
  • Image-based challenges: Users have to identify specific objects in a grid of images.
  • Audio-based challenges: Users must type the words they hear.
  • Puzzle challenges: Users are tasked to solve simple puzzles, such as navigating a maze.
Text CAPTCHA example

CAPTCHAs are often part of specific user flows, such as the final step of a form submission process:

captcha as a step of a form submission process example

In these cases, CAPTCHAs are there to ensure that bots do not complete the user flow. To automate these challenges, you can use CAPTCHA-solving libraries or services that rely on human operators to solve the challenges in real-time. However, hard-coded CAPTCHAs are uncommon due to their negative impact on user experience.

More frequently, CAPTCHAs are part of comprehensive anti-bot solutions, such as a WAF (Web Application Firewall):

Example-of-a-Web-Application-Firewall

These systems dynamically display CAPTCHAs when they suspect bot activity. In such cases, CAPTCHAs can be bypassed by making your bot mimic human behavior in a real-world browser. Still, this requires constantly updating your scripts to stay ahead of new bot detection methods.

A more effective solution for avoiding CAPTCHAs is to use a next-generation tool like Bright Data’s CAPTCHA solving feature. This cloud tool is always up-to-date and can handle a wide range of CAPTCHA types for you.

Selenium CAPTCHA Handling: Step-By-Step Tutorial

As you just learned, an effective way to avoid CAPTCHAs is to have your automated script mimic human behavior while controlling a browser with a real-world fingerprint. One of the best tools for that purpose is Selenium, a popular browser automation library.

In this tutorial section, you will learn how to avoid CAPTCHAs in Selenium using a Python script. Let’s get started!

Step #1: Create a New Python Project

Before getting started, make sure you have Python 3 and Chrome installed locally.

If you already have a Selenium web scraping or testing script, you can skip the first three steps. Otherwise, create a folder for your Selenium CAPTCHA bypass demo project and navigate to it in the terminal:

mkdir selenium_demo

cd selenium_demo

Next, add a new Python virtual environment inside it:

python -m venv venv

Open the project’s folder in your favorite Python IDE and create a new file named script.py.

Fantastic! Your project’s folder now contains a Python application.

Step #2: Install Selenium

Activate the Python virtual environment with the command below:

venv\Scripts\activate

Or, equivalently, if you are a Linux or macOS user:

source venv/bin/activate

Then, install Selenium through the pip package to selenium using this command:

pip install selenium

The installation process might take a while, so be patient.

Great! You are ready to initialize your Selenium script.

Step #3: Set Up Your Selenium Script

Import Selenium by adding the following line to script.py:

from selenium import webdriver

Now, create a ChromeOptions object to configure Chrome to start in headless mode:

options = webdriver.ChromeOptions()

options.add_argument("--headless")

If you are not familiar with this option, find out more in our guide on headless browsers.

Initialize a Chrome WebDriver instance with these options, and finally close it with quit(). This is what your current script.py file should currently look like:

from selenium import webdriver

# configure Chrome to start in headless mode

options = webdriver.ChromeOptions()

options.add_argument("--headless")

# start a Chrome instance

driver = webdriver.Chrome(options=options)

# browser automation logic...

# close the browser and release its resources

driver.quit()

The above script launches a new Chrome instance in headless mode before closing the browser. Awesome! It is time to implement the browser automation logic.

Step #4: Add the Browser Automation Logic

To evaluate Selenium CAPTCHA bypass logic, the automated script will connect to bot.sannysoft.com and take a screenshot. This special web page runs several tests in the browser to determine whether the user is a human or a bot. If you visit the page on your favorite browser, you will see that all the tests are passed.

Instruct the Chrome instance to visit the target page using the get() method:

driver.get("https://bot.sannysoft.com/")

Then, you need to take a screenshot of the entire page. Unfortunately, Selenium does not provide a function to achieve that directly. As a workaround, you can set the browser window to the width and height of the <body> node and then take a screenshot:

# get the body width and height

full_width = driver.execute_script("return document.body.parentNode.scrollWidth")

full_height = driver.execute_script("return document.body.parentNode.scrollHeight")

# set the browser window to the body width and height

driver.set_window_size(full_width, full_height)

# take a screenshot of the entire page

driver.save_screenshot("screenshot.png")

# restore the original window size

driver.set_window_size(original_size["width"], original_size["height"])

The above trick will do, and screenshot.png will contain the screenshot of the entire page.

Put it all together, and you will have the following logic:

from selenium import webdriver

# configure Chrome to start in headless mode

options = webdriver.ChromeOptions()

options.add_argument("--headless")

# start a Chrome instance

driver = webdriver.Chrome(options=options)

# connect to the target page

driver.get("https://bot.sannysoft.com/")

# get the current window size

original_size = driver.get_window_size()

# get the body width and height

full_width = driver.execute_script("return document.body.parentNode.scrollWidth")

full_height = driver.execute_script("return document.body.parentNode.scrollHeight")

# set the browser window to the body width and height

driver.set_window_size(full_width, full_height)

# take a screenshot of the entire page

driver.save_screenshot("screenshot.png")

# restore the original window size

driver.set_window_size(original_size["width"], original_size["height"])

# close the browser and release its resources

driver.quit()

Launch the script.py file above with this command:

python script.py

The script will start a Chromium instance in headless mode, connect to the desired page, take a screenshot, and close the browser. At the end of the script execution, a screenshot.png file will appear in the project root folder. Open it, and you will see:

screenshot.png file example

As you can tell by the red boxes, Chrome in headless mode controlled via vanilla Selenium does not pass various tests. That means your script is likely to be detected as a bot. The consequence is that a site protected with anti-bot technology may show a CAPTCHA when you interact with it. The solution to avoid a CAPTCHA in Selenium? The Stealth plugin!

Step #5: Install the Selenium Stealth Plugin

Selenium Stealth is a Python package designed to make the Chrome/Chromium instance controlled by Selenium less detectable as a bot. The goal of this project is to configure the browser to bypass almost all known bot detection strategies.

Specifically, Selenium Stealth modifies browser properties to prevent any leaks that expose the browser as automated. If you are familiar with anti-bot bypass solutions, this package can be seen as a re-implementation of Puppeteer Stealth.

Install Selenium Stealth via the pip package selenium-stealth:

pip install selenium-stealth

Then, import the library by adding this line to the script.py file:

from selenium_stealth import stealth

Here we go! All that remains is to configure the stealth settings.

Step #6: Configure the Stealth Settings to Avoid CAPTCHAs

To register Selenium Stealth and configure the Chrome WebDriver to avoid CAPTCHAs, call the stealth() function as follows:

stealth(

driver,

user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",

languages=["en-US", "en"],

vendor="Google Inc.",

platform="Win32",

webgl_vendor="Intel Inc.",

renderer="Intel Iris OpenGL Engine",

fix_hairline=True,

)

Configure the function arguments as you prefer, but keep in mind that the above values are enough to bypass most anti-bot measures.

Well done! The browser controlled by Selenium will now appear as a real-world browser used by a human user.

Step #7: Repeat the Bot Detection Test

Below is the final script.js file:

from selenium import webdriver

from selenium_stealth import stealth

# configure Chrome to start in headless mode

options = webdriver.ChromeOptions()

options.add_argument("--headless")

# start a Chrome instance

driver = webdriver.Chrome(options=options)

# configure the WebDriver to avoid bot detection

# with Selenium Stealth

stealth(

driver,

user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",

languages=["en-US", "en"],

vendor="Google Inc.",

platform="Win32",

webgl_vendor="Intel Inc.",

renderer="Intel Iris OpenGL Engine",

fix_hairline=True,

)

# connect to the target page

driver.get("https://bot.sannysoft.com/")

# get the current window size

original_size = driver.get_window_size()

# get the body width and height

full_width = driver.execute_script("return document.body.parentNode.scrollWidth")

full_height = driver.execute_script("return document.body.parentNode.scrollHeight")

# set the browser window to the body width and height

driver.set_window_size(full_width, full_height)

# take a screenshot of the entire page

driver.save_screenshot("screenshot.png")

# restore the original window size

driver.set_window_size(original_size["width"], original_size["height"])

# close the browser and release its resources

driver.quit()

Execute the bypass CAPTCHA Selenium Python script again:

python script.py

Take a look at screenshot.png, and you will notice that all bot detection tests have been passed:

All bot detection tests passed on the new screenshot.png

Et voilà! You now know the trick to avoid anti-bot CAPTCHAs in Selenium.

What to Do if the Above Bypass CAPTCHA Selenium Solution Does Not Work

Unfortunately, browser settings are not the only aspect that anti-bot solutions focus on. IP reputation is another key aspect, and you cannot simply change your IP with a more reliable one using a free library. That requires Selenium proxy integration!

In other words, even if you configure your browser optimally, CAPTCHAs may still show up. For simple CAPTCHAs that require only a single click, you can try to use packages like selenium-recaptcha-solver or selenium-recptcha. However, these libraries work only with reCAPTCHA v2 and are no longer maintained.

The main issue with the approach from the previous chapter and those packages is that they work only against basic CAPTCHAs. When dealing with more complex anti-bot systems like Cloudflare, you need a much more powerful solution.

Looking for a real Selenium CAPTCHA solution? Try Bright Data’s web scraping solutions!

These come with superior unlocking capabilities with a dedicated CAPTCHA-solving feature to automatically handle reCAPTCHA, hCaptcha, px_captcha, SimpleCaptcha, GeeTest CAPTCHA, FunCaptcha, Cloudflare Turnstile, AWS WAF Captcha, KeyCAPTCHA, and many others.

Integrating Bright Data’s CAPTCHA Solver into your script is easy, as it works with any HTTP client or browser automation tool—including Selenium.

Discover more about how to use Bright Data’s CAPTCHA Solver and check out the documentation for all configuration details.

Conclusion

In this article, you saw why CAPTCHAs are a challenge for automated software and how to address them in Selenium. Thanks to the Selenium Stealth library, you can override the default configurations of Chrome to limit bot detection. Yet, this approach is not a definitive solution.

Regardless of how sophisticated your Selenium CAPTCHA bypass logic is, advanced bot detection tools will still be able to block you. The real solution is to connect to the target site via an unblocking API that can return the CAPTCHA-free HTML of any web page.

That API is not a dream. It exists and is called Web Unlocker, a scraping API that automatically rotates your exit IP with each request via proxy integration and handles browser fingerprinting, automatic retries, and CAPTCHA resolution for you. Dealing with CAPTCHAs has never been easier!

Sign up now and start your free trial.

No credit card required