Let’s look at a practical example on how to solve CAPTCHAs using the DeathByCaptcha service. This example is written in C#, but you can get it in Java as well.

I use the Web Scraper Testing Ground as an example of a CAPTCHA-protected web page, but I recognize the first  CAPTCHA only as the most difficult one. Also since CAPTCHA images are generated dynamically I use Selenium WebDriver with Firefox to capture them.

If you don’t want to use the DeathByCaptcha service you can substitute it with some OCR software like GSA Capture Breaker. It simulates the service locally, allowing you to not pay per each captcha recognition.

I want to start explaining the code portion by portion, but if you don’t have time to read all the explanations you can find the complete code at the end of the post or download a ready project here.

OK, let’s start!

First, we need to initialize the WebDriver and open the target webpage with captcha:

Then we need to obtain the CAPTCHA image. Since the WebDriver doesn’t allow us to explicitly extract dynamically created images from the page, let’s create a screenshot of the whole page and then crop the captcha image basing on image position on the web page:

Now, since we have the CAPTCHA image, we can send it to the DeathByCaptcha service for further recognition:

After we receive a response from the service and the captcha is recognized, we can type its code into the textbox and click the “submit” button:

Then after the page reloads we can check whether the server accepted our captcha or not. If the captcha was recognized incorrectly, we need to send this report back to the DeathByCaptcha service:

That’s it! Here is the complete code of the application:

Don’t forget that you can  download the complete VS Project. And feel free to ask any questions or give any responses!