How to handle Captcha in Selenium

What is Captcha?

Through Captcha, the user is asked to perform a certain task, such as enter the text written in the image or click on the images from a set of different images that match the required criteria.

Handling Captcha Enabled Web Applications in Selenium

For web applications with Captcha enabled, automated UI testing could be an issue, especially as the step involving Captcha can create a hindrance in a test scenario. Moreover, the idea of Captcha itself does not align with automation, as doing so will defeat the purpose of prohibiting bots from performing activities on the application.

  1. By disabling the Captcha in the testing environment
  2. Adding a hook to click the Captcha checkbox
  3. By adding a delay to the Webdriver and manually solve Captcha while testing

How to Disable Captcha in the Test Environment

Disabling Captcha in the test environment would ensure that while testing a web application, the Captcha task is not required. While using Google’s open-source Captcha widget — reCAPTCHA v2, put in the test environment Site Key and the Secret Key, (known as the test keys). As a result, all verification requests will pass, and automated UI testing can be conducted seamlessly.

  • Site key: 6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI
  • Secret key: 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe

How to click the checkbox in reCaptcha during Selenium testing

By adding a hook to the code, users can bypass the Captcha while performing Automation Testing. The Captcha recognizes a bot clicking the checkbox element by the fact that it takes less time for the bot to click as compared to humans. Therefore, testers need to induce WebDriverWait, so that the element is clicked only when it becomes clickable. This bypasses the Captcha.

public class ReCaptcha_click { 

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");

ChromeOptions options = new ChromeOptions();

options.addArguments("start-maximized");

options.addArguments("disable-infobars");

options.addArguments("--disable-extensions");

WebDriver driver = new ChromeDriver(options);

driver.get("https://rsps100.com/vote/760");

new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]")));

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();

}

}
Captcha to access website

How to add delay and manually solve Captcha during Automation Testing

Firstly, this method is not completely automated and requires human intervention to solve the Captcha. Call it a semi-automated test.

import time time.sleep(10)
const puppeteer = require('puppeteer');

const chromeOptions = {

headless:false,

defaultViewport: null};

(async function main() {

const browser = await puppeteer.launch(chromeOptions);

const page = await browser.newPage();

await page.goto('https://old.reddit.com/login');

})()

await page.type('#user_reg', 'some_username');

await page.type('#passwd_reg', 'SuperStrongP@ssw0rd');

await page.type('#passwd2_reg', 'SuperStrongP@ssw0rd');

await page.click('#register-form button[type=submit]');

const chromeOptions = {

headless:false,

defaultViewport: null,

slowMo:15,

};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Garima Tiwari

Garima Tiwari

30 Followers

An avid reader and writer. Love to write about Tech, B2B Solutions, Culture, Arts, and Life