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:
1 2 3 |
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:
1 2 3 4 5 6 7 |
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:
1 2 3 4 5 6 |
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:
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:
1 2 3 4 5 6 7 8 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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!
Sonia
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
Michael Shilov
Hi Sonia, you can get the Java project here.
vincent
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
Michael Shilov
Yes, you need to have DeathByCaptcha’s account. I’m not aware about free good ways to solve captchas, are you?
Jim
Hi Michael
Can I contact you regarding some assistance?
Thanks
Michael Shilov
Sure Jim, use contact us page
Jason
I have a job for you. Would you please send me your email or phone number
Igor Savinkin
See the “Contact us” page if you are really interesting.
John
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:
Michael Shilov
Seems like that exception is related to bitmap cloning, not Chrome driver.
John
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);
Michael Shilov
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.
Chris
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
Michael Shilov
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.
Simba
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.
Simba
It is working now. I just updated the selenium to the latest version.
Azad
using DeathByCaptcha;
can you please tell how to get this particular dll ?
Aneesh
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;
mahi
Hi
How can do it for ruby.
Could you please suggest any alternate solutions to solve image captcha in ruby.
Igor Savinkin
Hi Mahi,
I’m not a ruby programmer, so you’d better turn directly to the DeathByCaptcha service.
Kiran
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
M Adeel
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.
Jacob
I need help writing a captcha solver for a specific use, can you help?
CEMIL
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?
Igor Savinkin
Have you checked an image id? Is it exactly ‘captcha’? Post a link to your target site.
Tiok
Hello
how about use in imacros ?
Tammy
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
Igor Savinkin
I think you need to check a particular captcha solving service that you’re interesting in: See this post.
Santosh
Hi, I am trying to break the new nocaptcha/recaptcha with python and dbc. Do you have the code for python?
Igor Savinkin
I do not. But you may try brute force to solve new reCaptcha.
Thomas
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.
Igor Savinkin
it might be possible.
Esteban
How do I solve a case sensitive captcha??
sam
getScreenshotAs() method is missing in driver class. i got no method error. please suggest . where do i get this method.