---
title: "How to Bypass CAPTCHAs With Selenium in Python"
slug: bypass-captchas-with-selenium
date: 2024-07-16T08:20:54+00:00
modified: 2025-09-16T16:39:50+00:00
permalink: https://brightdata.com/blog/web-data/bypass-captchas-with-selenium
type: blog
---

[ Blog ](https://brightdata.com/blog "Blog") / [Web Data](https://brightdata.com/blog/web-data)







 [Web Data](https://brightdata.com/blog/web-data)

# 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





 [ ](https://brightdata.com/blog/authors/antonello-zanini)

 [Antonello Zanini

Technical Writer

 ](https://brightdata.com/blog/authors/antonello-zanini)





 ![How to bypass CAPTCHAs with Selenium blog image](https://media.brightdata.com/2024/07/How-to-bypass-CAPTCHAs-with-Selenium.svg)





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](/blog/web-data/what-is-a-captcha#:~:text=A%20CAPTCHA%20is%20a%20challenge,solve%20but%20difficult%20for%20machines.), 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.

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

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](https://www.cloudflare.com/learning/ddos/glossary/web-application-firewall-waf/)[ Application Firewall](https://www.cloudflare.com/learning/ddos/glossary/web-application-firewall-waf/)):

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](/webinar/bot-detection).

A more effective solution for avoiding CAPTCHAs is to use a next-generation tool like Bright Data’s [CAPTCHA solving feature](/products/web-unlocker/captcha-solver). 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](https://www.selenium.dev/), 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](https://www.python.org/downloads/) and [Chrome](https://www.google.com/chrome/) installed locally.

If you already have a [Selenium web scraping](/blog/how-tos/using-selenium-for-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:

```none
mkdir selenium_demo

cd selenium_demo
```

Next, add a new [Python virtual environment](https://docs.python.org/3/library/venv.html) inside it:

```none
`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](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#activate-a-virtual-environment) with the command below:

```none
`venvScriptsactivate`
```

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

```none
`source venv/bin/activate`
```

Then, install Selenium through the `pip` package to [`selenium`](https://pypi.org/project/selenium/) using this command:

```none
`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`:

```none
`from selenium import webdriver`
```

Now, create a [`ChromeOptions`](https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.chrome.options) object to configure Chrome to start in headless mode:

```none
options = webdriver.ChromeOptions()

options.add_argument("--headless")
```

If you are not familiar with this option, find out more in our guide on [headless browsers](/blog/proxy-101/what-is-a-headless-browser).

Initialize a [Chrome WebDriver](https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.chrome.webdriver) instance with these options, and finally close it with `quit()`. This is what your current `script.py` file should currently look like:

```none
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](https://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()`](https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.get) method:

```none
`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:

```none
# 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:

```none
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:

```none
`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:

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](https://github.com/diprajpatra/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](/blog/how-tos/avoid-getting-blocked-with-puppeteer-stealth).

Install Selenium Stealth via the `pip` package [`selenium-stealth`](https://pypi.org/project/selenium-stealth/):

```none
`pip install selenium-stealth`
```

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

```none
`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:

```none
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:

```none
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:

```none
`python script.py`
```

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

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](/integration/selenium)!

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](https://pypi.org/project/selenium-recaptcha/) or [`selenium-recptcha`](https://pypi.org/project/selenium-recaptcha/). 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](/blog/brightdata-in-practice/how-to-bypass-captcha-using-web-unlocker) and check out the [documentation](https://docs.brightdata.com/scraping-automation/web-unlocker/introduction) 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](/products/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.



Contact usStart free trial

No credit card required











 [ ](https://www.linkedin.com/in/antonello-zanini/)

Antonello Zanini

 Technical Writer



  5.5 years experience



Antonello Zanini is a technical writer, editor, and software engineer with 5M+ views. Expert in technical content strategy, web development, and project management.



Expertise

  Web Development   Web Scraping   AI Integration



 [ View all articles ](https://brightdata.com/blog/authors/antonello-zanini)











 Table of Contents







CAPTCHA Solver

Easily bypass all CAPTCHAs with Web Unlocker. No more getting blocked.

[See pricing](/pricing/web-unlocker "See pricing")

Rotating Proxies

Avoid CAPTCHAs with the best rotating proxies.

[See pricing](/pricing/proxy-network/residential-proxies "See pricing")







 [ ](https://news.ycombinator.com/submitlink?t=How+to+Bypass+CAPTCHAs+With+Selenium+in+Python&u=https://brightdata.com/blog/web-data/bypass-captchas-with-selenium) [ ](https://www.linkedin.com/shareArticle?mini=true&title=How+to+Bypass+CAPTCHAs+With+Selenium+in+Python&url=https://brightdata.com/blog/web-data/bypass-captchas-with-selenium) [ ](http://www.reddit.com/submit?title=How+to+Bypass+CAPTCHAs+With+Selenium+in+Python&url=https://brightdata.com/blog/web-data/bypass-captchas-with-selenium)







##  You might also be interested in

 [ ](https://brightdata.com/blog/web-data/bypass-captchas-with-cypress "How to Bypass CAPTCHAs With Cypress")

 [Web Data





Antonello Zanini

Technical Writer





### How to Bypass CAPTCHAs With Cypress

Discover how to handle CAPTCHAs in Cypress, including effective bypass methods and what to do when CAPTCHAs persist, ensuring seamless browser automation.



 11-Jul-2024

 8 min read

 ](https://brightdata.com/blog/web-data/bypass-captchas-with-cypress)

 [ ](https://brightdata.com/blog/web-data/bypass-captchas-with-playwright "How to Bypass CAPTCHAs With Playwright")

 [Web Data





Antonello Zanini

Technical Writer





### How to Bypass CAPTCHAs With Playwright

Learn how to bypass CAPTCHAs using Playwright and ensure your web scraping tasks run smoothly without interruptions.



 09-Jul-2024

 8 min read

 ](https://brightdata.com/blog/web-data/bypass-captchas-with-playwright)

 [ ](https://brightdata.com/blog/how-tos/avoid-getting-blocked-with-puppeteer-stealth "Avoid Getting Blocked With Puppeteer Stealth")

 [How Tos





Antonello Zanini

Technical Writer





### Avoid Getting Blocked With Puppeteer Stealth

Learn how to integrate Puppeteer Stealth into a puppeteer scraping script to avoid getting blocked.



 05-Dec-2023

 7 min read

 ](https://brightdata.com/blog/how-tos/avoid-getting-blocked-with-puppeteer-stealth)
