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:
using (var driver = new FirefoxDriver()) { driver.Navigate().GoToUrl("http://testing-ground.scraping.pro/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:
var arrScreen = driver.GetScreenshot().AsByteArray; using (var msScreen = new MemoryStream(arrScreen)) { var bmpScreen = new Bitmap(msScreen); var cap = driver.FindElementById("captcha"); var rcCrop = new Rectangle (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:
using (var msCaptcha = new MemoryStream()) { imgCap.Save(msCaptcha, ImageFormat.Png); // put your DeathByCaptcha credentials here var client = new SocketClient("user", "password"); var res = 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:
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:
var h4 = driver.FindElementByXPath("//div[@id='case_captcha']//h4"); if (!h4.Text.Contains("SUCCESSFULLY")) { Console.WriteLine("The captcha has been solved incorrectly!"); client.Report(res); } else Console.WriteLine("The captcha has been solved correctly!");
That’s it! Here is the complete code of the application:
using System; using System.Drawing; using System.Drawing.Imaging; using OpenQA.Selenium.Firefox; using DeathByCaptcha; using System.IO; namespace CaptchaSolver { class Program { static void Main(string[] args) { Console.WriteLine("Initializing the Firefox webdriver..."); using (var driver = new FirefoxDriver()) { Console.WriteLine("Opening the page..."); driver.Navigate().GoToUrl("http://testing-ground.scraping.pro/captcha"); var arrScreen = driver.GetScreenshot().AsByteArray; using (var msScreen = new MemoryStream(arrScreen)) { var bmpScreen = new Bitmap(msScreen); var cap = driver.FindElementById("captcha"); var rcCrop = new Rectangle (cap.Location, cap.Size); Image imgCap = bmpScreen.Clone(rcCrop, bmpScreen.PixelFormat); using (var msCaptcha = new MemoryStream()) { imgCap.Save(msCaptcha, ImageFormat.Png); // put your DeathByCaptcha credentials here var client = new SocketClient("user", "password"); Console.WriteLine("Sending request to DeathByCaptcha..."); var res = client.Decode(msCaptcha.GetBuffer(), 20); if (res!=null && res.Solved && res.Correct) { driver.FindElementByXPath ( "//input[@name='captcha_code']" ).SendKeys(res.Text); driver.FindElementByXPath ( "//input[@name='submit']" ).Click (); var h4 = driver.FindElementByXPath("//div[@id='case_captcha']//h4"); if (!h4.Text.Contains("SUCCESSFULLY")) { Console.WriteLine("The captcha has been solved incorrectly!"); client.Report(res); } else Console.WriteLine("The captcha has been solved correctly!"); } else Console.WriteLine("Captcha recognition error occured"); } } } Console.ReadKey(); } } }
Don’t forget that you can download the complete VS Project. And feel free to ask any questions or give any responses!
37 replies on “How to Write a Captcha Solver that uses DeathByCaptcha service”
HI,
Can you provide me the code in java for crop the captcha image basing on image position on the web page.
Or can you provide me the whole above code in JAVA as it is on C# ..it would be very helpful to me..
Thanks in Advance
Hi Sonia, you can get the Java project here.
Hi
Can i get Python code for the same
I think you should turn to DBC.
I’m a complete nubie – how do I use this file? Are you saying that I need to sign up for death by captcha – run this file (how) and I won’t have to pay a price for captcha solving?
Please Advise
Yes, you need to have DeathByCaptcha’s account. I’m not aware about free good ways to solve captchas, are you?
Hi Michael
Can I contact you regarding some assistance?
Thanks
Sure Jim, use contact us page
I have a job for you. Would you please send me your email or phone number
See the “Contact us” page if you are really interesting.
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:
Seems like that exception is related to bitmap cloning, not Chrome driver.
How to substitute it with GSA Capture Breaker ?
I know I have to change it here but no idea how to.
using (var msCaptcha = new MemoryStream())
{
imgCap.Save(msCaptcha, ImageFormat.Png);
// put your DeathByCaptcha credentials here
var client = new SocketClient(“user”, “password”);
var res = client.Decode(msCaptcha.GetBuffer(), 20);
John, you don’t need to change the code if you want to use GSA Capcha Breaker. It intercepts calls to DeathByCaptcha calls and answers instead of it.
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 🙂
Hi Chris, I wrote an article on how to do it in Java. The usage is similar, but you need to use their C# API instead.
Hi,
The program is stuck after console display ‘Initializing the Firefox webdriver…’. It does open the firefox browser but then nothing happen.
Please do guide me to right direction 😀
Thank you.
It is working now. I just updated the selenium to the latest version.
using DeathByCaptcha;
can you please tell how to get this particular dll ?
Hi
I am new to c# selenium.
Can you please tell how to set up the DeathByCaptcha in a C# project ?
its written as a API in death by captcha site.
How to use the command and load the dll ?
using DeathByCaptcha;
Hi
How can do it for ruby.
Could you please suggest any alternate solutions to solve image captcha in ruby.
Hi Mahi,
I’m not a ruby programmer, so you’d better turn directly to the DeathByCaptcha service.
Hi,
I tried to run this but i get a pop up to download or search for a file called DriverServices.cs. But there is no such file in my system.
Could you please help me on this?
Thanks
Kiran
i think you didn’t correctly set the location from where it will get your driver.exe file. try to locate the file will solve your issue.
I need help writing a captcha solver for a specific use, can you help?
Hi, I cannot crop the captcha successfully using the code below.
var bmpScreen = new Bitmap(msScreen);
var cap = driver.FindElementById(“captcha”);
var rcCrop = new Rectangle (cap.Location, cap.Size);
Image imgCap = bmpScreen.Clone(rcCrop, bmpScreen.PixelFormat);
Its working on many sites including yours in example code, but not working on my target site. I saved the image cropped, its empty. What could be the problem?
Have you checked an image id? Is it exactly ‘captcha’? Post a link to your target site.
Hello
how about use in imacros ?
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
I think you need to check a particular captcha solving service that you’re interesting in: See this post.
Hi, I am trying to break the new nocaptcha/recaptcha with python and dbc. Do you have the code for python?
I do not. But you may try brute force to solve new reCaptcha.
I was wondering if you could help me write a program that solves the 9by9 caps. And then fills out of a form and submits the data.
it might be possible.
How do I solve a case sensitive captcha??
getScreenshotAs() method is missing in driver class. i got no method error. please suggest . where do i get this method.
Hey mate!
How can I change DeathByCaptcha for GSA?
Regards!