How to automate the captcha

Here is the stepwise working solution 

Step1- Add dependency for net.sourceforge.tess4j 

Step2- Download and keep “eng.traineddata” file in your project directory 

Step3- 

//Create one folder to store captcha image and assign that path to Variable Stringpath_stud_captcha=”C:/Users/shrad/.eclipse/Cucumber/src/test/resources/captcha_images/c ap.png”; 

//Inspect and store captcha element 

WebElement wb_stud=driver.findElement(By.xpath(“//div[@class=’style_captchaContainer   LdFYB’]”)); 

//Give some wait 

Thread.sleep(3000); 

//Get screenshot of captcha element 

File src_stud = wb_stud.getScreenshotAs(OutputType.FILE); 

//Store the screenshot 

FileHandler.copy(src_stud, new File(path_stud_captcha)); 

//extract the captcha text from screenshot image using OCR 

ITesseract img_stud = new Tesseract(); 

String value_stud=img_stud.doOCR(new File(path_stud_captcha)); 

//Store extracted text into string 

String cap1=value_stud.replaceAll(“[^a-zA-Z1-9]+”, “”); 

//Use 1st 4 Characters as captcha 

String cap_stud=cap1.substring(0, 4); 

// Send that string to captcha’s field 

driver.findElement(By.id(“captcha”)).sendKeys(cap_stud); 

Leave a comment

Your email address will not be published. Required fields are marked *