Can Captcha be Automated in Selenium (Complete Guide)

Can captcha be automated in Selenium
Can captcha be automated in Selenium

As we all know, digital security has become one of the critical element in today’s world. There are plenty of ways to achieve it successfully, and Captcha is one of them. A Captcha test aka Completely Automated Public Turing test to tell Computers and Humans Apart, is a type of digital security test that helps the websites to differentiate between Humans and Bots so that it can protect user’s sensitive data and save them from harmful cyber-attacks. Honestly speaking, it is very easy for a human to solve these challenges, but hard for bots and malicious software without the help of advanced technology. Although, if you are a software tester and are using Selenium as your test tool, then Captcha’s might be bad news for you. Plus, it would eat up your precious time and energy, while you can use it for another task. So, is there any way to solve this problem? Can captcha be automated in Selenium? 

Well, let’s find out!

Contents

Can Captcha be automated in Selenium?

No, a Captcha cannot be automated in Selenium as it will totally ruin the use of Captcha test, i.e., maintaining digital security. However, there are some workarounds that you could try to handle Captcha in Selenium.

So, if you want to try them out, then keep reading.

How to handle Captcha in Selenium?

No doubt a developer cannot automate the Captcha in Selenium, but he/she can use these three workarounds that can help them to minimize the effort.

By disabling Captcha in the test environment:

The easiest way to get rid of the Captcha codes during the testing period is by disabling them because disabling the Captcha during the test environment will completely eliminate the Captcha solving task. In fact, you can use Google’s very own open-source Captcha widget called “reCAPTCHA” which will help you in bypassing the Captcha in the testing environment.

Not to mention, if you are using reCAPTCHA v2, then you can use the “Site key and Secret Key” also known as “Test keys”, to turn off the Captcha verification process. 

Here are the keys:

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

Moreover, if you are using reCAPTCHA v3, then you can create different keys for a separate testing environment. 

However, when the Captcha is disabled in the test environment, you won’t be able to get a clear idea of how it looks when used in the live environment. 

By clicking the reCAPTCHA box in Selenium:

If your application is equipped with the reCAPTCHA checkbox, then you can click on the Captcha to complete the script. The mechanism that reCAPTCHA uses is that it checks the time frame used to complete a Captcha test because humans take more time to complete the challenge compared to bots.

Therefore, a tester needs to persuade WebDriverWait, so that delay can be added to replicate the human behavior.

By adding a delay in the Selenium script and manually solving Captcha while testing. 

Lastly, you can add a delay in the Selenium script. But remember, as mentioned in the title, it requires human interaction to solve the Captcha, and therefore it can be called a “semi-automated test.” 

By using this method, a delay of 10 to 20 seconds can be introduced to freeze the Captcha screen so that the page doesn’t load up fast, which is again considered to be a bot behavior.

So, by implementing delay to the test, it ensures that by the time reCAPTCHA is visibly ready, the form filling action can be halted and the racing condition of the webpage can be removed, thus making it more accurate.

If you are using Python and want to delay the execution of Selenium WebDriver for 10 seconds, then use the following command:

import time

time.sleep(10)

But, if you are using Puppeteer, then you can use the following command to delay the execution:

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,

};

What kind of tests are possible on Captcha in Selenium?

Captcha plays an important role while running a website, and it is crucial while using Selenium too. Therefore, let’s try and find the possible tests that could be carried out on Captcha while using Selenium.

  • To verify that the application accepts only valid captcha.
  • To make sure that the Captcha code is case-sensitive.
  • To verify that an error message is shown on the screen if the Captcha code is incorrect.
  • To verify that the Captcha code is visible on the screen.
  • To verify that a new Captcha code gets generated every time the page is refreshed.
  • To make sure that a new Captcha code is generated every time the user enters the wrong Captcha code.
  • To make sure that the copy-paste feature is disabled for the Captcha input box.
  • To verify that the application Is not accepting partial Captcha code.

Final Remarks on “Can Captcha be automated in Selenium?”

There is no doubt that Captcha’s are lifesavers of the digital world as it protects your websites from automated bots & malicious software. But if you are a software tester and are using Selenium to test the web applications, then it could actually waste your time and energy. Therefore, you can opt for the solutions that are mentioned in the article to minimize your efforts in Captcha solving and focus on the primary task, i.e., web app testing.