如何使用Selenium WebDriver和Java从图像(验证码)中读取文本

如何使用Selenium WebDriver和Java从图像(验证码)中读取文本

问题描述:

我有注册网页,但最后验证码显示..

I have registration webpage but in last captcha is displaying..

我无法从图像中读取文字。我将提及代码和输出..

I am not able to read the text from image. I am going to mention the code and output ..

@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);
}

输出:错误


线程main中的异常org.openqa.selenium.NoSuchElementException:
无法找到元素:
{method:xpath ,选择器:// [@ id ='forgotPassword'] / table / tbody / tr [5] / td [3] / img}
命令持续时间或超时:60.02秒有关此错误的文档,请访问:
http://seleniumhq.org/exceptions/no_such_element.html 构建信息:
版本:'2.35.0',修订版:'8df0c6b',时间:'2013-08-12 15:43:19'
系统信息:os.name: 'Windows 7',os.arch:'amd64',os.version:
'6.1',java.version:'1.6.0_26'会话ID:
5f5b2e1a-56a4-49ad-8fd3-2870747a7768驱动程序信息:
org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform = XP,
acceptSslCerts = true,javascriptEnabled = true,browserName = fi refox,
rotate = false,locationContextEnabled = true,version = 23.0.1,
cssSelectorsEnabled = true,databaseEnabled = true,handlesAlerts = true,
browserConnectionEnabled = true,nativeEvents = true,
webStorageEnabled = true,applicationCacheEnabled = true,
takesScreenshot = true}]
sun.reflect.NativeConstructorAccessorImpl.newInstance0(原生方法)
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)引起:
org.openqa.selenium.remote.ErrorHandler $ UnknownServerException:Unable
找到元素:
{method:xpath,selector://
[@ id ='forgotPassword'] / table / tbody / tr [5] / td [3] / img}
构建信息:版本:'2.35.0',修订版:'8df0c6b',时间:'2013-08-12
15:43:19'系统信息:os .name:'Windows 7',os.arch:'amd64',
os.version:'6.1',java.version:'1.6.0_26'驱动程序信息:
driver.version:unknown at .FirefoxDriver.prototype .findElementInternal_(file:/// C:/Users/lukup/AppData/Local/Temp/anonymous4043037924964932185webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:8880)
at .fxdriver.Timer .prototype.setTimeout /< .notify(文件:/// C:/Users/lukup/AppData/Local/Temp/anonymous4043037924964932185webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:396)

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/fxdriver@googlecode.com/components/driver_component.js:8880) at .fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/lukup/AppData/Local/Temp/anonymous4043037924964932185webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:396)


为了详细说明以前的答案,CAPTCHA作为完全自动公共图灵测试的首字母缩写告诉计算机和人类分开。
所以,如果机器可以解决它,那就不是真正的工作。

Just to elaborate the previous answers, CAPTCHA as an acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart". So, if "machine" can solve it, it's not really do it's job.

为了解决它,你可以做些什么 - 到使用外部服务的API,例如 http://www.deathbycaptcha.com
您实现他们的API,将CAPTCHA传递给他们并获得文本。我观察到的平均解算时间约为10-15秒。

In order to solve it, there is something you can do - to use API of external services such as http://www.deathbycaptcha.com. You implementing their API, passing them the CAPTCHA and get in return the text. The average solving time i have observed is around 10-15 seconds.

实施示例(摘自这里

import com.DeathByCaptcha.AccessDeniedException;
import com.DeathByCaptcha.Captcha;
import com.DeathByCaptcha.Client;
import com.DeathByCaptcha.SocketClient;
import com.DeathByCaptcha.HttpClient;

/* Put 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 your CAPTCHA file name, or file object, or arbitrary input stream,
       or an array of bytes, and optional solving timeout (in seconds) here: */
    Captcha captcha = client.decode(captchaFileName, timeout);
    if (null != captcha) {
        /* The CAPTCHA was solved; captcha.id property holds its numeric ID,
           and captcha.text holds its text. */
        System.out.println("CAPTCHA " + captcha.id + " solved: " + captcha.text);

        if (/* check if the CAPTCHA was incorrectly solved */) {
            client.report(captcha);
        }
    }
} catch (AccessDeniedException e) {
    /* Access to DBC API denied, check your credentials and/or balance */
}