3 ways or method of handling reCAPTCHA in selenium

3 Ways or Methods of handling reCAPTCHA in Selenium

The term “CAPTCHA” refers to a “Completely Automated Public Turing Test to Tell Computers and Humans Apart.” It has been designed, as the name implies, to prevent any automation process. Therefore, automation QA testers working in the computer software and IT industries often find it difficult to automate reCAPTCHA through automation testing. For all web applications that have enabled the reCAPTCHA module, doing UI (user interface) automation testing with Selenium could face several problems or issues. Thus, it is very important, necessary, and we can even say mandatory to handle reCAPTCHA issues in solving this problem.

In this blog, we will look at three useful ways to handle reCAPTCHA when using Selenium for automation.

Handling of reCAPTCHA through Selenium

Automating web applications for testing is essential in today’s fast-paced development environment. Selenium, a powerful tool for automating web applications, is crucial in this process. However, automation testers often face challenges in dealing with reCAPTCHA. reCAPTCHA, which is designed to distinguish between human users and operating systems, is a major challenge for automation scripts.

3 Ways Or Methods Of Handling reCAPTCHA In Selenium

While testing a web application with a reCAPTCHA enabled feature module, automation test engineers using the Selenium automation software testing tool can solve or handle the reCAPTCHA problem in three ways or methods: –

1. Through disabling or deactivating the reCAPTCHA in the test environment itself of the application.

2. Through clicking on the reCAPTCHA checkbox through Selenium Automation Testing Software Tool.

3. By adding delay or wait commands provided by Selenium like Implicit Wait, Explicit Wait & Fluent Wait so that it can resolve the issue or problem of reCAPTCHA during the execution or running of our Selenium Automation test scripts which is a semi-automated process because this method requires some manual intervention as well and complete automation is not possible.

Now let’s discuss these 3 ways or methods to handle or solve the problem of reCAPTCHA in Selenium one by one.

First Method – Disabling or Deactivating reCAPTCHA in the test environment.

Disabling or Deactivation of reCAPTCHA in the test environment of a web application whether it is QA, Staging and Production while testing a web application as it would ensure that use of reCAPTCHA is not required. QA Testers can ask developer of a particular web application to disable the Captcha module in the test environment.

In case of application having reCAPTCHA v2, QA Testers can use the generic test keys namely site key and secret key to ensure no CAPTCHA is required and all verification requests made through CAPTCHA should be passed automatically.

In case of application having reCAPTCHA v3, it requires separate keys for all testing environments which QA Testers can create him/herself. So in this way QA Testers can bypass reCAPTCHA and UI automation testing with Selenium Automation Testing Software Tool can be done or conducted seamlessly & successfully. This method will ensure that reCAPTCHA has been bypassed successfully and it can only be used for testing purposes of the application with no real time access for the users of the application.

Second Method – Clicking on reCAPTCHA Checkbox with the help of Selenium Automation Tool.

QA testers can bypass reCAPTCHA issue or problem with the help of Selenium Automation Testing Software Tool by automating reCAPTCHA Checkbox to click on it.

QA testers have to use WebDriverWait method which is a type of Explicit Wait so the checkbox element is clicked only when it becomes clickable through automation with Selenium.

In this way the Captcha problem or issue can be bypassed or resolved successfully.

Here is the Sample code to automate reCAPTCHA Checkbox with the help of Selenium Automation Testing Software Tool using Java as Programming Language for writing automation test script :-

public class ReCaptcha Checkbox Click

{

public static void main(String args[])

{

System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\PrecisePC\\Documents\\chromedriver.exe”);

ChromeOptions opt = new ChromeOptions();

options.addArguments(“start-maximized”);

options.addArguments(“disable-infobars”);

options.addArguments(“disable-extensions”);

WebDriver driver = new ChromeDriver(opt);

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

WebDriverWait wait = new WebDriverWait(driver, 30);

Wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt

(By.xpath(“//iframe[starts-with(@name, ‘a-‘) and starts-with

(@src,  ‘https://www.google.com/recaptcha‘)]”)));

WebDriverWait wait1 = new WebDriverWait(driver, 30);

Wait1.until(ExpectedConditions.elementToBeClickable

(By.cssSelector(“div.recaptcha-checkbox-checkmark”))).click();

}

}

Third Method – Use of Selenium delay or wait commands like Implicit Wait, Explicit Wait & Fluent Wait.

In this method QA testers can use Selenium delay or wait commands which are very important and necessary for our Selenium Automation Test Scripts.

This method is not fully automated as it requires some manual intervention as well so we can say that it is a semi-automated test.

In this method a delay or wait of 10-20 seconds is important and necessary so that the web page shouldn’t suffer from race condition which usually happens on doing automation of a Captcha element.

Here is a sample code to automate captcha in Selenium using delay or wait commands with Java as programming language to write automation test script:-

public class Selenium ReCaptcha Automation

{

public static void main(String args[])

{

System.setProperty(“webdriver.gecko.driver”, “C:\\Users\\PrecisePC\\Documents\\geckodriver.exe”);

FirefoxOptions opt = new FirefoxOptions();

options.addArguments(“start-maximized”);

options.addArguments(“disable-infobars”);

options.addArguments(“disable-extensions”);

WebDriver driver = new FirefoxDriver(opt);

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

WebDriverWait wait = new WebDriverWait(driver, 10);

Wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt

(By.xpath(“//iframe[starts-with(@name, ‘a-‘) and starts-with

(@src,  ‘https://www.google.com/recaptcha‘)]”)));

WebDriverWait wait1 = new WebDriverWait(driver, 20);

Wait1.until(ExpectedConditions.elementToBeClickable

(By.cssSelector(“div.recaptcha-checkbox-checkmark”))).click();

}

}

Conclusion

While reCAPTCHA are an important security protocol for distinguishing between humans and bots, they can pose major challenges for selenium-based automation. The methods described above provide a variety of ways to handle different reCAPTCHA in automation testing. It is important to choose the method that best meets your testing needs and ethical considerations. Remember that the goal of automation is to streamline the testing process while maintaining the integrity and security of the application being tested.

For more information, visit our website at www.precisetestingsolution.com or call our office at 0120-368-3602. Also, you can send us an email at info@precisetestingsolution.com.

We look forward to helping your business grow!

Robotic Process Automation (RPA)
March 16, 2024

Robotic Process Automation (RPA): A Complete Guide

Explore the world of Robotic Process Automation (RPA): its

Locators in Selenium 
November 14, 2023

A Detailed Guide to Locators in Selenium

In this blogpost everything you need to know about

Precise Testing Solution Pvt Ltd