Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Selenium by (7k points)
edited by

We have a registration webpage but in last captcha is displaying and not able to read text from image.

My code

@Test
public void loginTest() throws InterruptedException {
    System.out.println("Testing");
    driver.get("https://customer.onlinelic.in/ForgotPwd.htm");

    WebElement element = driver.findElement(By.xpath("//*[@id='forgotPassword']/table/tbody/tr[5]/td[3]/img"));
    System.out.println(" get the instance ");

    String elementTest = element.getAttribute("src");
    System.out.println("Element : " + elementTest);
}

Error

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//[@id='forgotPassword']/table/tbody/tr[5]/td[3]/img"} Command duration or timeout: 60.02 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19' System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_26' Session ID: 5f5b2e1a-56a4-49ad-8fd3-2870747a7768 Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=23.0.1, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307) at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:404) at org.openqa.selenium.By$ByXPath.findElement(By.java:344) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299) at seleniumtest.CaptchaTest.loginTest(CaptchaTest.java:41) at seleniumtest.CaptchaTest.main(CaptchaTest.java:59) Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"xpath","selector":"//[@id='forgotPassword']/table/tbody/tr[5]/td[3]/img"} Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19' System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_26' Driver info: driver.version: unknown at .FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/lukup/AppData/Local/Temp/anonymous4043037924964932185webdriver-profile/extensions/[email protected]/components/driver_component.js:8880) at .fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/lukup/AppData/Local/Temp/anonymous4043037924964932185webdriver-profile/extensions/[email protected]/components/driver_component.js:396)

2 Answers

0 votes
by (31.9k points)

A sample code you can use 

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.awt.image.RenderedImage;

import java.io.File;

import java.io.IOException;

import java.net.URL;

import javax.imageio.ImageIO;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

import com.asprise.util.ocr.OCR;

public class ExtractImage {

 WebDriver driver;

 @BeforeTest

  public void setUpDriver() {

   driver = new FirefoxDriver();

  }

 @Test

 public void start() throws IOException{

 /*Navigate to http://www.mythoughts.co.in/2013/10/extract-and-verify-text-from-image.html page

  * and get the image source attribute

  *  

  */  

 driver.get("http://www.mythoughts.co.in/2013/10/extract-and-verify-text-from-image.html");

 String imageUrl=driver.findElement(By.xpath("//*[@id='post-body-5614451749129773593']/div[1]/div[1]/div/a/img")).getAttribute("src");

 System.out.println("Image source path : \n"+ imageUrl);

 URL url = new URL(imageUrl);

 Image image = ImageIO.read(url);

 String s = new OCR().recognizeCharacters((RenderedImage) image);

 System.out.println("Text From Image : \n"+ s);

 System.out.println("Length of total text : \n"+ s.length());

 driver.quit();

 /* Use below code If you want to read image location from your hard disk   

  *   

   BufferedImage image = ImageIO.read(new File("Image location"));   

   String imageText = new OCR().recognizeCharacters((RenderedImage) image);  

   System.out.println("Text From Image : \n"+ imageText);  

   System.out.println("Length of total text : \n"+ imageText.length());   

   */ 

}

}

Want to learn Selenium, checkout our Selenium training. 

0 votes
by (2.8k points)

CAPTCHA representing "Completely Automated Public Turing test to tell Computers and Humans Apart".In order to solve it, there is something you can do  that is to use the API of external services such as http://www.deathbycaptcha.com. You implement their API, passing them the CAPTCHA and get in return the text. 

Example for implementation:
import com.DeathByCaptcha.AccessDeniedException;
import com.DeathByCaptcha.Captcha;
import com.DeathByCaptcha.Client;
import com.DeathByCaptcha.SocketClient;
import com.DeathByCaptcha.HttpClient;
/* Insert your DeathByCaptcha account username and password here.
Use HttpClient for HTTP API. */
Client client = (Client)new SocketClient(username, password);
try {
double balance = client.getBalance();
/* Put the CAPTCHA file name, or file object,or an array of bytes, and optional solving timeout (in seconds) below: */
Captcha captcha = client.decode(captchaFileName, timeout);
if (null != captcha) {
/* The CAPTCHA was resolved; captcha.id property has its numeric ID,
and captcha.text has its text. */
System.out.println("CAPTCHA " + captcha.id + " solved: " + captcha.text);
if (/* to check if the CAPTCHA was solved incorrect */) {
client.report(captcha);
}
}
} catch (AccessDeniedException e) {
/*Your Access to DBC API is denied,kindly check your balance */
}

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...