Did someone ask how to handle captcha in selenium? Okay, I heard it! Let’s see how these can be handled using Selenium WebDriver and yeah, we will try getting this done quickly as I will have to shoot off to a contest later today.
Prepare to be lit up
CAPTCHA is a bacronym for “Completely Automated Public Turing test to tell Computers and Humans Apart”.
Oh, don’t get mad at me! That was not a typo! There really is something called a bacronym/backronym. When an abbreviation is formed from the initials of other words, it is called an acronym. But if a new phrase is created to fit already existing initials, then it is called a bacronym.
So now that you know what a bacronym is and what a CAPTCHA stands for, lets get into the details. Captchas are mainly used to determine whether the user is a human or not. It generates images or some form of tests that humans can solve but bots/computer programs cannot.
So its main purpose is to prevent bots and other automated programs from obtaining any sensitive information from the website. Hence captcha exists to kill automation! If we are able to automate it, then the reason of using captchas become worthless.
Do I have anything up my sleeve? Let me see…
- For the pupose of testing, we can always request the developers to
- Disable captcha validation
- Provide a workaround or backdoor for obtaining the captcha value
- Tweak captcha such that it accepts one specific input value everytime
- Develop a way, where a random text gets stored in the alt attribute of the captcha image which can then be obtained and passed into the captcha text box using available locators
But be very sure that these are ONLY active in the testing environment and are used ONLY for the purpose of automation.
Note that the application security is getting compromised while testing using these methods mentioned above.
- If you want to test the application as it is, i.e. without making any modifications to the testing environment for the purpose of automation, then separate all test scripts that involve captcha validation and package them as a single test suite. Run this test suite with human intervention. I know this is partial automation but it is not always possible to automate everything under a given set of circumstances. To get something you should be ready to forego something.
- Entire test case except captcha can be automated.
- An implicit or an explicit wait can be used and user can be promped to enter the displayed captcha.
- When the test script is run, each step will be executed as usual. Once the prompt command is reached, a pop up appears on the browser. User enters the captcha as displayed on the screen.
- Once the captcha is entered manually, the test case execution resumes and the subsequent steps are run on the browser.
This was the procedure I followed in one of my previous projects. Because this method makes sure that the application under test is exactly the one that will be pushed into production and also the security is not compromised in any way.
Let us look at an example to get a better understanding.
Scenario
- Open Firefox browser
- Navigate to https://www.in.ckgs.us/myaccount/
- Locate ‘Current Passport Number’ text box by cssSelector: tag and name attribute
- Enter ‘123456789’
- Prompt the user to intervene and enter the displayed captcha
- Locate the captcha text box by id
- Send the user entered value to the located captcha text box
- Verify JUnit pane for success result in eclipse IDE
JUnit code for this scenario is,
package com.blog.junitTests;
import java.util.concurrent.TimeUnit;
import javax.swing.JOptionPane;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CaptchaValidation {
//Declaring variables
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception{
// Selenium version 3 beta releases require system property set up
System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");
// Create a new instance for the class FirefoxDriver
// that implements WebDriver interface
driver = new FirefoxDriver();
// Implicit wait for 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// Assign the URL to be invoked to a String variable
baseUrl = "https://www.in.ckgs.us/myaccount/";
}
@Test
public void testPageTitle() throws Exception{
// Open baseUrl in Firefox browser window
driver.get(baseUrl);
// Locate 'Current Passport Number' text box by cssSelector: tag and name attribute
WebElement passportNo = driver.findElement(By.cssSelector("input[name='currentPassportNo']"));
// Clear the default placeholder or any value present
passportNo.clear();
// Enter/type the value to the text box
passportNo.sendKeys("123456789");
//prompting user to enter captcha
String captchaVal = JOptionPane.showInputDialog("Please enter the captcha value:");
//Type the entered captcha to the text box
driver.findElement(By.id("recaptcha_response_field")).sendKeys(captchaVal);
}
@After
public void tearDown() throws Exception{
// Close the Firefox browser
driver.close();
}
}
Execution result
The comments are clearly provided for each line of code.
String captchaVal = JOptionPane.showInputDialog("Please enter the captcha value:");
JoptionPane pops up a standard dialog box and showInputDialog prompts user for some input. Once the user enters the displayed captcha and clicks ‘OK’, it will be saved to the string “captchaVal”.
driver.findElement(By.id("recaptcha_response_field")).sendKeys(captchaVal);
This value saved in ‘captchaVal’ will be entered in the captcha text box located by id.
In JUnit pane, green bar shows that the test case is executed successfully.
Below image shows the final output executed in Firefox browser.
I got you all covered on captchas! See you again in another post.
Have a great day!
Atlast someone made my day .. I want to thank u from core of my heart….really worthful post for me
Welcome
Manual intervention is not required ,If we want to enter manually what is the need of writing of code like JOptionPane?
Hi Rajesh,
That’s a great question. Only simple methods of handling a captcha are discussed here. Anyways, the reason for writing code like JOptionPane is to stop the automation at the step where captcha is expected and providing the user with a small simple UI to enter the displayed captcha manually, by displaying a custom message. It serves not only as a breakpoint but also ensures to serve as a prompt telling the user that manual intervention is required at this point to proceed further. The code example provided in this post might just have few simple steps but in real time projects, an automated script may consist of 250 to 300 steps. In such scenarios, it is beneficial to request manual intervention For one step rather than to execute everything manually.
After running this code getting exception :
Exception in thread “main” org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified
(Session info: chrome=75.0.3770.100)
Please help
if we entered wrong captcha then how it will work please explain.
Hi Snehal,
It will depend on how the website under test responds to the wrong captcha. Most of the times, the captcha will be refreshed and this can be handled in the automation script with a try/catch block.
can you please help me to handle Google captcha?? http://prntscr.com/ntu8e8
Hi Bindu,
I checked the link you specified. Captcha is not clickable and it is just rendered as an image. You can verify that by inspecting the element using Chrome Developer Tools (F12), which will show you that `img` tag is used for captcha. The cursor turns into a magnifier when hovered on the captcha. It can be automated only if it is clickable in the first place.
Thanks.
Helpful l solution.
Thank you so much
You are welcome, Salman.
Very very innovative and helpful
Happy it helped you, Ajay.
Its really helpfull,,, thank you so much