---
title: Handling Captcha during Automated Testing
description: We will discuss possible solutions to run automated tests with Captcha
  enabled websites.
source_url:
  html: https://testingbot.com/resources/articles/automated-testing-with-captcha
  md: https://testingbot.com/resources/articles/automated-testing-with-captcha.md
---

![Handling Captcha during Automated Testing](https://testingbot.com/assets/resources/articles/7.webp)

# How to handle captchas during your automated tests

We will discuss possible solutions to run automated tests with Captcha enabled websites.

By [Jochen D.](https://testingbot.com/about/jochen-d)2021-08-24

 Share on Facebook 

 Share on Twitter 

 Post on Reddit 

 Share link 

Handling captchas (Completely Automated Public Turing test to tell Computers and Humans Apart) with Selenium is a problem where some testers struggle to find a solution for.   
 If you are wondering how to handle reCaptcha with Selenium or any other captcha with Selenium, this guide will help with a couple of possible solutions.

We will discuss possible solutions to run automated tests with Captcha enabled websites.

## Table of Contents

- [What is a Captcha?](https://testingbot.com#introduction)
- [How to disable captchas in your staging environment?](https://testingbot.com#disable)
- [How to click the reCaptcha checkbox with Selenium WebDriver?](https://testingbot.com#click)
- [How to manually solve a captcha during your automated tests?](https://testingbot.com#manually)
- [How to handle Captcha's with Headless Puppeteer & Playwright?](https://testingbot.com#headless)
- [How to handle invisible reCaptcha with automated tests?](https://testingbot.com#invisible)

## What is a Captcha?

A Captcha is a task, asked to the user of the website or application.   
 The task usually involves entering text from an image, performing a simple calculation or matching certain images from a group of mixed images.

The Captcha is similar to a puzzle, used to differentiate humans from automated bots.   
 The presented captcha is dynamically generated, it's built to be easy to solve by humans and hard to solve by aritificial intelligence (AI) bots.

The most popular captchas are recaptchas (built by Google).   
 ReCaptcha is popular because it's easy to integrate by webdevelopers and offers [accessibility features](https://support.google.com/recaptcha/answer/6175971) such as screen-readers and voice-overs (audio challenge).

## How to disable captchas in your staging environment?

The easiest and most foolproof way to handle captchas, is to disable the need for captchas during your test. By disabling the captcha, your automated tests do not need to be modified and are not required to enter the captcha response.

The disadvantage of this is approach is that you are testing something which is different from your production environment. Ideally, you want your automated tests to test your product on an environment most closely resembling to your production environment.

If you want to run [automated tests using reCaptcha](https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha.-what-should-i-do), you can choose to use separate test keys (`Site key` and `Secret key`). The reCaptcha will show a big red warning to the user (automated test), warning that the captcha is disabled and should not be used in production.

It is recommended to safeguard your code and credentials in such a way that the test credentials can never be used in production mode.

While in reCaptcha test mode, reCaptcha will accept any answer to the captcha challenge.   
 Your automated test can click or input any answer, the captcha will always allow the request.

## How to click the reCaptcha checkbox with Selenium WebDriver?

During your automated test, you'll want to click the captcha to complete the action.   
 Once of the mechanisms that reCaptcha uses is checking how long it took for the captcha to complete.   
 Humans will take a bit longer to complete a captcha task as compared to robots.   
 Before clicking the captcha, you will want to add a delay (for example using `WebDriverWait`) to mimic human behavior.

Please see the example below, where our automated test will detect the captcha, wait a couple of seconds and proceed to complete the captcha.

```java
driver.get("https://testingbot.com/");
new WebDriverWait(driver, 10).until(
	ExpectedConditions.frameToBeAvailableAndSwitchToIt(
		By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]")
	)
);
// wait a couple of seconds before clicking the Captcha
new WebDriverWait(driver, 20).until(
	ExpectedConditions.elementToBeClickable(
		By.cssSelector("div.recaptcha-checkbox-checkmark")
	)
).click();
```

## How to manually solve a captcha during your automated tests?

We do not recommend this option, as it requires manual intervention during an automated test, which means your automated tests are not really automated anymore.

With this method, your test will detect the captcha and wait for a specific amount of time, allowing you to enter the correct captcha answer.

While waiting for the captcha answer, you will need to manually look at the captcha question, solve the captcha and pass the answer to the (automated) test.

```ruby
require "rubygems"
require "selenium-webdriver"
require "selenium/client"

caps = {
  :browserName => "internet explorer",
  :version => "8",
  :platform => "WINDOWS"
}

urlhub = "https://hub.testingbot.com/wd/hub"
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120

@webdriver = Selenium::WebDriver.for :remote, :url => urlhub, :desired_capabilities => caps, :http_client => client
@webdriver.navigate.to "https://testingbot.com"
captcha = @webdriver.find_element(:name => "captcha")
sleep 50
answer = gets #get user input
captcha.send_keys(answer)

@webdriver.quit
```

## How to handle Captcha's with Headless Puppeteer & Playwright?

If you are using Puppeteer or Playwright instead of Selenium, chances are you are also looking for a way to click a captcha.

With Puppeteer and Playwright you can open an URL containing the captcha, find the specific captcha DOM element and click the captcha:

```javascript
const puppeteer = require('puppeteer')

const browser = await puppeteer.connect({
  browserWSEndpoint: 'wss://cloud.testingbot.com?key=key&secret=secret&browserName=chrome&browserVersion=latest'
})

const page = await browser.newPage()
await page.goto('https://testingbot.com')

const captchaElement = await page.$('div.recaptcha-checkbox-checkmark')
await captchaElement.click()
browser.close()
```

Another solution is to use [puppeteer-extra-plugin-recaptcha](https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-recaptcha), which is a Puppeteer plugin to solve captchas (reCAPTCHAs and hCaptchas).

## How to handle invisible reCaptcha with automated tests?

reCAPTCHA v2 offers an invisible captcha, which does not require the user to perform any interaction with the captcha.

The invisible captcha will try to block web traffic that appears to come from a bot, so in order to avoid this, we can:

- Change the user-agent of our test script.
- Add random waits to mimic user behavior.

### Change the user-agent

Selenium, Puppeteer and Playwright allow you to change your user-agent.   
 For example, while using Python with Selenium, you can use a Firefox profile and override the `general.useragent` flag:

```javascript
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "Mozilla/5.0 (Windows NT x.y; rv:10.0) Gecko/20100101 Firefox/52.0")
driver = webdriver.Firefox(profile)
```

### Add random waits

Adding random waits to your automated tests, between various steps of your test, is easy to accomplish:

```ruby
require "rubygems"
require "selenium-webdriver"
require "selenium/client"

caps = {
  :browserName => "internet explorer",
  :version => "8",
  :platform => "WINDOWS"
}

urlhub = "https://hub.testingbot.com/wd/hub"
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120

@webdriver = Selenium::WebDriver.for :remote, :url => urlhub, :desired_capabilities => caps, :http_client => client
@webdriver.navigate.to "https://testingbot.com"
# ... perform action
sleep rand(5..10) # wait randomly for 5 up to 10 seconds
# ... perform another action

@webdriver.quit
```

## Sidebar

### TestingBot Cloud Testing

Run automated, manual and visual tests on remote browsers and devices. Sign up for a free trial.

[Free Trial](https://testingbot.com/users/sign_up)

### Latest articles

[![Cypress and Cucumber Browser Testing](https://testingbot.com/assets/resources/articles/6-495c5e14cb4bf1e282858fc7e2cfcbe89775e3790b88d34c15bf9a6f36d387d4.webp)](https://testingbot.com/resources/articles/cypress-cucumber-testing)

#### [Cypress and Cucumber Browser Testing](https://testingbot.com/resources/articles/cypress-cucumber-testing)

Cypress and Cucumber is a great combination to write clea...

[Read article →](https://testingbot.com/resources/articles/cypress-cucumber-testing)

[![TestNG automation with Selenium](https://testingbot.com/assets/resources/articles/5-46a5444bcdd6b07d6bbe8e9e58be5093fe3aaa8d1a7939b89291d7173d4e4c74.webp)](https://testingbot.com/resources/articles/testng-selenium)

#### [TestNG automation with Selenium](https://testingbot.com/resources/articles/testng-selenium)

TestNG is a popular Java Test Framework which allows you ...

[Read article →](https://testingbot.com/resources/articles/testng-selenium)

[![blank](https://testingbot.com/assets/blank-765bda2d39d03b623c68515d78259503eb9ca884105d9fff58b2693418720ea1.gif)](https://testingbot.com/resources/articles/cross-browser-testing-competitors)

#### [TestingBot vs Browserstack and SauceLabs - Competitor analysis](https://testingbot.com/resources/articles/cross-browser-testing-competitors)

Why would you choose TestingBot instead of an other compa...

[Read article →](https://testingbot.com/resources/articles/cross-browser-testing-competitors)

## Other Articles

[![Cypress and Cucumber Browser Testing](https://testingbot.com/assets/resources/articles/6-495c5e14cb4bf1e282858fc7e2cfcbe89775e3790b88d34c15bf9a6f36d387d4.webp)](https://testingbot.com/resources/articles/cypress-cucumber-testing "Cypress and Cucumber Browser Testing")

### [Cypress and Cucumber Browser Testing](https://testingbot.com/resources/articles/cypress-cucumber-testing)

Cypress and Cucumber is a great combination to write clean and precise end-to-end browser tests.

[Read article →](https://testingbot.com/resources/articles/cypress-cucumber-testing)

[![TestNG automation with Selenium](https://testingbot.com/assets/resources/articles/5-46a5444bcdd6b07d6bbe8e9e58be5093fe3aaa8d1a7939b89291d7173d4e4c74.webp)](https://testingbot.com/resources/articles/testng-selenium "TestNG automation with Selenium")

### [TestNG automation with Selenium](https://testingbot.com/resources/articles/testng-selenium)

TestNG is a popular Java Test Framework which allows you to write clean Selenium tests with built-in parallelisation.

[Read article →](https://testingbot.com/resources/articles/testng-selenium)

[![blank](https://testingbot.com/assets/blank-765bda2d39d03b623c68515d78259503eb9ca884105d9fff58b2693418720ea1.gif)](https://testingbot.com/resources/articles/cross-browser-testing-competitors "TestingBot vs Browserstack and SauceLabs - Competitor analysis")

### [TestingBot vs Browserstack and SauceLabs - Competitor analysis](https://testingbot.com/resources/articles/cross-browser-testing-competitors)

Why would you choose TestingBot instead of an other company like SauceLabs or BrowserStack?

[Read article →](https://testingbot.com/resources/articles/cross-browser-testing-competitors)

[![blank](https://testingbot.com/assets/blank-765bda2d39d03b623c68515d78259503eb9ca884105d9fff58b2693418720ea1.gif)](https://testingbot.com/resources/articles/automated-testing "Automated website testing with TestingBot and Selenium")

### [Automated website testing with TestingBot and Selenium](https://testingbot.com/resources/articles/automated-testing)

With a large number of different browsers platforms and mobile devices it is very important to make sure your website looks and behaves the way you want it to across all these different browsers.

[Read article →](https://testingbot.com/resources/articles/automated-testing)

## Ready to start testing?
[Start a free trial](https://testingbot.com/users/sign_up)
