Automating captcha using Selenium and Tessaract
<b>Problem Statement:</b>
Automating captcha in web pages without downloading the captcha image.
<b>Technical Stack:</b>
Selenium
C#
Tessaract OCR
<b>Solution:</b>
Tessaract OCR:
Tessaract is an open source OCR (Optical Character Recognition) engine developed by Google. It is used to extract text from image.
The latest Nuget package for Tessaract can be found in the below link
https://www.nuget.org/packages/Tesseract/
(PM> Install-Package Tesseract -Version 3.0.2)
Download and install latest version of Tessaract from the below link
<i>https://digi.bib.uni-mannheim.de/tesseract/ </i>
Copy the “tessdata” folder from the installed location and paste in the project directory.
<b>Selenium:</b>
Identify the captcha image using Selenium WebDriver.
The captured WebElement (Captcha Image) has to be passed to Tessaract class to extract the text from image.
<b>Source Code:</b>
public static string GetCaptchaText(IWebElement element)
{
var ocrText = string.Empty;
try
{
ITakesScreenshot screenshotDriver = ReferenceBrowser as ITakesScreenshot;
Byte[] arrScreen = screenshotDriver.GetScreenshot().AsByteArray;
var msScreen = new MemoryStream(arrScreen);
Bitmap screen = new Bitmap(msScreen);
//to get absolute point of the element
IJavaScriptExecutor executor = (IJavaScriptExecutor)ReferenceBrowser;
var yOffsetObject = executor.ExecuteScript(“return window.pageYOffset;”);
int offsetY = Convert.ToInt16(yOffsetObject);
Point point = new Point();
point.X = element.Location.X;
point.Y = (element.Location.Y – offsetY);
var rectCrop = new Rectangle(point, element.Size);
Image imgCap = screen.Clone(rectCrop, screen.PixelFormat);
Bitmap imgSource = new Bitmap(imgCap);
//Point to the tessdata location and define locale.
var engine = new TesseractEngine(System.IO.Directory.GetParent(BasePath).Parent.FullName +”\\tessdata”, “eng”, EngineMode.TesseractAndCube);
var img = PixConverter.ToPix(imgSource);
var page = engine.Process(img);
ocrText = page.GetText().ToString().Trim().Split(‘\n’)[0];
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
return ocrText;
}
<b>LIMITATIONS:</b>
Can only automate only simple captcha which are clear to read.
Accuracy level is 90% *
*Accuracy varies according to the captcha image and the Tessaract version.