Guvi-blog-logo

How To Automate Captcha In Selenium?

what is captcha

Automating UI testing for web applications becomes tricky when Captcha comes into the picture. In fact, the whole point of enabling Captcha itself does not fall in line with automation. As we all know, Captcha intends to prohibit bots from interfering in the application. And that’s the reason why Captcha can create a hindrance in a test scenario. So, how can we go ahead and automate Captcha with selenium? Is it even possible?  

It is inevitable to handle Captcha efficiently, to ensure that tests run without pullbacks. 

what is captcha?

Well, to be upfront automating Captcha is not the best practice and it is not advisable from the security perspective. But you can try these three efficient ways of handling Captcha in Selenium:

  1. Introduce a delay to the Webdriver & then manually solve Captcha while testing
  2. Disable the Captcha in the testing environment
  3. Add a hook to click the Captcha checkbox

Find out: Who Is An Automation Testing Engineer? & How To Become One?

Before we walk through each of these methods, let’s understand the following:

What is Captcha?

Automation is at its best in this digital era. But like every coin has two sides, these automation strategies can reverse the effect when taken over by Cybercriminals. As a prevention to these malicious attacks, Captcha or “Completely Automated Public Turing test to tell Computers and Humans Apart” is commonly used.

In short, Captcha is an important UI element that helps in restricting bots from accessing a web application. However, it is posing a major challenge for Automation Experts performing UI testing using Selenium. It is like, you purposely build something to create hindrance in the process and it in turn produces hindrance in the whole purpose! Funny!

To verify whether the user is a human or a bot, the user is put to certain tasks via Captcha. The tasks may include:

Enter some disfigured text:

what is captcha

or

Click on a set of similar images matching the image in the Captcha:

captcha handling in selenium

Moreover, some variants of Captcha also support visually impaired individuals. They generate audio instead of images.

Now, the trick is that the task is DYNAMIC. Yes! Only humans can take a call and answer the Captcha correctly. The correct interpretation of information is something that a bot fails to produce.

captcha automation in selenium

Handling Captcha Enabled Web Applications in Selenium

Since automating Captcha would beat the complete purpose of holding Captcha in the first place to confirm human interaction with the UI, it is very tricky to manage Captcha in Selenium. 

  • Introduce a delay to the Web driver & then manually solve Captcha while testing
  • Let me tell you upfront that the technique of adding delay to the web driver is only a semi-automatic test; the reason being, that the user anyhow would have to solve and then enter the captcha manually.

    Delay in code

    In this technique, you have to introduce a delay of 10 to 20 secs. Thereby, freezing the Captcha screen. Usually, when we implement automation testing on a web page, there is a time-lapse. It happens because automation testing happens to be much faster than the reCaptcha. So, while the Captcha screen is still frozen, we hold enough time to enter the password and automate the whole process without the Captcha break! That’s wonderful, isn’t it?

    Furthermore, using Python, you can easily insert a delay with time.sleep() command. You should definitely code this one!

    Wish to learn about Selenium Automation Testing: Get Started here!

  • Disable the Captcha in the testing environment
  • This is a simple tactic wherein we make sure that while testing a web application, the Captcha task is blocked-out! Yes! You can bypass Captcha altogether but only during the testing process is ongoing. By inserting the Site Key and the Secret Key, in Google’s open-source Captcha widget. Therefore, all verification requests will pass without any hindrances. And, you can automate your UI testing with no hurdles. 

    Login No Captcha reCAPTCHA – WordPress plugin | WordPress.org

    Most importantly, while doing so, the reCAPTCHA widget will show a warning alert. This will ensure that the CAPTCHA is bypassed, notably only for testing purposes. & BLING! Captcha would be back! So, this is a good option which will prevent Captcha while the Automation process takes place, bringing back Captcha, once the automation process gets over.

    Interested in Automation Testing: Learn everything here!

  • Add a hook to click the Captcha checkbox
  • This is another brilliant method of bypassing the Captcha. We can add a hook to the code.

    Do you know, how does Captcha recognize a bot clicking the checkbox element? It’s actually simple! The bot takes less time to click as compared to humans.

    Captchas: An Annoying Illustration of Static Versus Dynamic Security —  Chris Louie, CISSP

    Now, this is the reason why the automation testing Experts need to induce WebDriverWait. So, the element is clicked only when it becomes clickable. This, in turn, bypasses the Captcha. Problem solved!

    So, now do you know captcha handling in selenium?

    You may automate your processes by inducing one of the above three methods and take care of captcha handling in selenium. Now that you know what is captcha and how to do captcha automation in selenium, are you intrigued to take Automation Testing to the next level? Then find link to our Automation Testing Program here

    Have any doubt? Share it in the comment section below. We will get back to you shortly!

    Contact Form

    By clicking 'Submit' you Agree to Guvi Terms & Conditions.

    Our Learners Work at

    Our Popular Course

    Share this post

    Author Bio

    Archana
    Archana
    A traveler, and explorer, Archana is an active writer at GUVI. You can usually find her with a book/kindle binge-reading (with an affinity to the mystery/humor).

    Our Live Classes

    Learn Javascript, HTML, CSS, Java, Data Structure, MongoDB & more
    Learn Python, Machine Learning, NLP, Tableau, PowerBI & more
    Learn Selenium, Python, Java, Jenkins, Jmeter, API Testing & more
    Learn Networking, Security Testing, IAM, Access Management & more

    Hey wait, Don’t miss New Updates from GUVI!

    Get Your Course Now

    Related Articles