Scroll to top

9q. WebDriver – Handling a CAPTCHA

Did someone say captchas? 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…

  1. For the pupose of testing, we can always request the developers to
    1. Disable captcha validation
    2. Provide a workaround or backdoor for obtaining the captcha value
    3. Tweak captcha such that it accepts one specific input value everytime
    4. 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.

  1. 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.
    1. Entire test case except captcha can be automated.
    2. An implicit or an explicit wait can be used and user can be promped to enter the displayed captcha.
    3. 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.
    4. 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.


  1. Open Firefox browser
  2. Navigate to
  3. Locate ‘Current Passport Number’ text box by cssSelector: tag and name attribute
  4. Enter ‘123456789’
  5. Prompt the user to intervene and enter the displayed captcha
  6. Locate the captcha text box by id
  7. Send the user entered value to the located captcha text box
  8. Verify JUnit pane for success result in eclipse IDE

JUnit code for this scenario is,


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;

			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 = "";
			public void testPageTitle() throws Exception{
				// Open baseUrl in Firefox browser window
				// 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
				// Enter/type the value to the text box
				//prompting user to enter captcha			
				String captchaVal = JOptionPane.showInputDialog("Please enter the captcha value:");
				//Type the entered captcha to the text box
			  public void tearDown() throws Exception{
				// Close the Firefox browser

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”.


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!

Author avatar
Chandana Chaitanya
Lakshmi Chandana is a Software professional + passion-fueled blogger + novel-reader + artist + tutor to make your day a little brighter than it was before! She is thrilled you are here! She is on a mission to make sure learning sticks but with the fun part kept intact. She uses certain tricks called BrainBells (inspired from barbells and dumbbells used for workout) to achieve this and she says, “this is not the hardest job! As once minions said, it for sure is working in a bubble wrap factory. Imagine the self-control needed!” So, dive in to explore the fun-filled World of Learning!!


  1. minal patil

    Its really helpfull,,, thank you so much

  2. Ajay

    Very very innovative and helpful

  3. Salman

    Helpful l solution.
    Thank you so much

  4. Bindu

    can you please help me to handle Google captcha??

    • Chandana Chaitanya

      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.


  5. snehal

    if we entered wrong captcha then how it will work please explain.

    • Chandana Chaitanya

      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.

  6. Hemant

    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

  7. Rajesh

    Manual intervention is not required ,If we want to enter manually what is the need of writing of code like JOptionPane?

    • Chandana Chaitanya

      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.

Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.