26

How can I automate "Enter CAPTCHA" using Selenium WebDriver?

  • 30
    If you could automate CAPTCHA, it would mean that it's a very poor implementation of CAPTCHA. – vsz Feb 15 '16 at 15:01
  • 22
    I feel like you've missed the point of CAPTCHA. – Ryan Feb 15 '16 at 17:38
  • What do you need to use it for? Do you have a site with a CAPTCHA component that you still want to be able to test with Selenium, possibly by selectively disabling the CAPTCHA when testing? – Milo P Feb 15 '16 at 19:31
  • In my opinion, you need a very powerful image processing algorithm and SikuliX. Selenium doesn't suit this. – Buaban Feb 16 '16 at 4:52
  • 1
    Are they using Sony's CAPTCHA? Your problem is trivial if so. (The worst part is that it's still there and contains the same problem 5 years later.) – jpmc26 Feb 16 '16 at 23:13

13 Answers 13

46

You don't, that is the whole idea behind a good CAPTCHA.

CAPTCHA stands for:

Completely Automated Public Turing test to tell Computers and Humans Apart

So, by definition, resolving a CAPTCHA cannot be automated: Otherwise it could not tell computers and humans apart and hence fails being a CAPTCHA.

Howto handle a CAPTCHA in a test environment:

  1. If you need to test an application which uses CAPTCHA, then you need to get the development team to build in a workaround/backdoor which is only active in the test environment.
  2. Use a service like http://www.deathbycaptcha.com which has an API to return the text of the CAPTCHA for you. Average response is 15 seconds with a result rate of 90% or so they say. Sounds like something to try.
  • 4
    Unlike other answers there you show a good point: "a good captcha": many captcha implementations are easy to break (old page, but has many examples). Of course breaking the captcha isn't an recommended at all, but if someone got here because wants to use some captcha to prevent automation and want to know how someone can automate it then is important to know when a captcha is good or not. – Gustavo Rodrigues Feb 15 '16 at 11:35
  • There are some captchas that has been automated ;-) See my answer. – dzieciou Jan 27 '17 at 12:32
37

We cannot automate CAPTCHA, as it is not meant to be automatized.

How to deal with CAPTCHA?

  1. Either ask dev team for a workaround, like configure CAPTCHA in test environment in such a way it will always accept 1 specific value.

  2. Ask developer to disable the CAPTCHA module in testing environment.

  3. If your are using custom CAPTCHA module, you can ask developer to generate an API of CAPTCHA generation for testing environment.

  4. You can ask dev team to add CAPTCHA code as title in markup, then you can access this title and bypass the CAPTCHA, but only in the testing environment.

Most of the time we go with option 1 or 2.

NOTE: When you do work around for CAPTCHA in test environment, You should always have this point in your checklist to make sure workarounds for CAPTCHA testing are removed before it goes on production.

  • 7
    I'd like to emphasize something about all three of your points: for a testing environment. None of these should ever be put into your production environment, ever, because otherwise you break the whole point of CAPTCHA. – Nic Hartley Feb 16 '16 at 20:09
8

Here's an example of captcha and a robot to cheat it:

captcha

5

As other posters explained, whole point of CAPTCHA is that recognition cannot be automated with our current pattern recognition programs. Even as AI algorithms solving pattern recognition will improve, new CAPTCHA will be developed.

So what you need is automated humans - a web service which outsources solving CAPTCHA, for like $1 per 1000 CAPTCHA solved. You post image to the service API, real humans in some country with cheap labor solve them within seconds, and send you answer back.

Long live human ingenuity!

  • 1
    +1 I wanted to give a similar answer, but services like mturk.com seem to frown upon breaking CAPTCHA's with the use of humans, but still for testing purposes it should be fine. – Niels van Reijmersdal Feb 15 '16 at 19:32
3

If you're trying to automate a CAPTCHA like Google reCAPTCHA, well, you just can't. Simple reason being the CAPTCHA code is updated and added to very frequently.

You may be able to automate some other CAPTCHA that use a limited database. Such CAPTCHA will have a limited number of code. You can somehow make a list of all the available codes and their IDs. I'm sure they'll have some sort of unique identification. Once you have that data, while automating it you can fetch the ID of the captcha being called, search in your list and filling in the value associated with that CAPTCHA in the text field. I remember this being told to me by Santhosh Tuppad a couple of years ago.

To automate modules that make use of CAPTCHA field you can,

  1. Ask your development team to remove the CAPTCHA field on the test environment.

  2. Ask them to make a sandbox like CAPTCHA, that will accept a single input text every time.

  3. Ask them to make a working model sandbox of the CAPTCHA, where each time the CAPTCHA text will be generated dynamically, but the same text will also be inserted in the text field or there is some way for you to know the text field generated, so that you can insert that in the text field.

3

For simple CAPTCHAs like the one below you can first extract the image with Selenium and then send it to the free OCR.space OCR API for processing. This will return the text inside the (Captcha) image.

OCR will return "Input this code: Z X 7 Z"

Another option for solving simple Captchas, and for running image/video-related tests in general, is to use the Kantu Browser (like Sikuli, but for the web browser). It has built-in OCR features.

2

There are tools and services to accomplish this, 75% of the time. The question reminded me of a piece of software called JDownloader, a download manager with CAPTCHA bypass abilities. According to their thread on the topic, they support many services in addition to their own automated OCR tool.

Some services they use:

Their tool, JAntiCaptcha, in SearchCode.

Depending on your language, integrating one service or another may be more challenging, so be sure to do your research.

2

How can I automate, 'Enter CAPTCHA' using Selenium Web Driver ?

Presumably you don't really want to test the CAPTCHA feature, you just want your automation to go past that portion of your application, so that you can test the remainder using automation.

If that's the case, you need to get your developers to give you a "back-door" - basically a test-only version of the page that will let you bypass the CAPTCHA feature and continue on.

Talk to your developers. Explain your predicament. Ask them to propose a solution.

In the past, I've had developers give me a "replacement" page for testing. It omitted the CAPTCHA but contained everything else.

2

The best way to work with Captcha in Selenium would be to give a wait time for an element that is loaded after entering the captcha, enter the captcha manually and then proceed further. The part of captcha entering cannot be automated unless something very complex is used.

2

You can bypass No CAPTCHA reCAPTCHA - details are on the Google Dev FAQ:

https://developers.google.com/recaptcha/docs/faq#what-happens-to-recaptcha-v1

1

I am presuming that you want to create automation for validating CAPTCHA, & you are familiar with automation with Databases.

Captcha is same concept in terms of generating automatically like OTP. It has been generating in back end and some scripting mechanism used to wrap those codes in to configuration which can be identify by human , only .

So In order to automate testing of it you can divide into two parts , one is automation using just getting back end CAPTCHA Data and store in variable and validate by inputting into captcha block using webDrviver & pass that captcha challenge. while other practice you need to verifying that captcha to be solve its purpose for human readable form only , you need to do manual testing for verification.

1

What I have done to deal wit CAPTCHA, is to test on a dummy site but the CAPTCHA was a static one. I do not agree with the people here saying to remove it because they site layout will be affected.

1

To summarise the above:

  1. you shouldn't that's the point of captcha.
  2. implement strong AI to mimic human capabilities
  3. implement a test harness to bypass captcha in your test automation suite

protected by Kate Paulk Mar 16 '18 at 11:18

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.