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:

C#

1

2

3

4

5

6

7

vararrScreen=driver.GetScreenshot().AsByteArray;

using(varmsScreen=newMemoryStream(arrScreen))

{

varbmpScreen=newBitmap(msScreen);

varcap=driver.FindElementById("captcha");

varrcCrop=newRectangle(cap.Location,cap.Size);

Image imgCap=bmpScreen.Clone(rcCrop,bmpScr.PixelFormat);

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

C#

1

2

3

4

5

6

using(varmsCaptcha=newMemoryStream())

{

imgCap.Save(msCaptcha,ImageFormat.Png);

// put your DeathByCaptcha credentials here

varclient=newSocketClient("user","password");

varres=client.Decode(msCaptcha.GetBuffer(),20);

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:

C#

1

2

3

4

5

6

7

if(res!=null&&res.Solved&&res.Correct)

{

driver.FindElementByXPath("//input[@name='captcha_code']")

.SendKeys(res.Text);

driver.FindElementByXPath("//input[@name='submit']")

.Click();

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:

I have a job for you. Would you please send me your email or phone number

Igor SavinkinApr 24, 2017 @ 08:48

See the “Contact us” page if you are really interesting.

JohnMar 09, 2014 @ 14:08

How to do this with Chrome driver? I try to do it with chrome but I got this error. System.OutOfMemoryException was unhandled
Message=Out of memory.
Source=System.Drawing
StackTrace:
at System.Drawing.Bitmap.Clone(Rectangle rect, PixelFormat format)
at CaptchaSolver.Program.Main(String[] args) in C:\realcaptcha\Program.cs:line 35
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

How can I make this code work with Bypass CAPTCHA service?
What changes I need to do to the code?
Is this the only line I need to change ?
var client = new SocketClient(“user”, “password”);
Please help me

Is there a downloadable script written in javascript for any of the captcha services, other than 9kw? DBC, Imagetyperz? Any?? The .js sample from 9kw works perfectly in imacros, however its so slow, it usually times out before solving the new recaptcha 2.0’s. (usually solves 50% of the time) I know ZERO about programing or script writing and would love to have a javascript that I could just plop my username and password into (like 9kw) and let it run! Any help would be greatly appreciated! I tried editing the 9kw js file to suit DBC and I failed because I do not understand how to write code/scripts