我有注册网页,但在最后一个验证码显示..
我无法从图像中读取文本 . 我要提一下代码和输出..
@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);
}
Output: Error
线程“main”中的异常org.openqa.selenium.NoSuchElementException:无法定位元素:{“method”:“xpath”,“selector”:“// [@ 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功能[{platform = XP,acceptSslCerts = true,javascriptEnabled = true,browserName = firefox,rotate = false,locationContextEnabled = true,version = 23.0.1,cssSelectorsEnabled = true,databaseEnabled = true,handlesAlerts = true,browserConnectionEnabled = true,nativeEvents = true,webStorageEnabled = true,applicationCacheEnabled = true,takesScreenshot = true}] at at sun.reflect.NativeConstructorAccessor Impl.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)org.openqa.selenium.remote上的org.openqa.selenium.remote.ErrorHandler.crelenThrowable(ErrorHandler.java:191)atg.openqa.selenium.remote.Error.Arn .rmoteWebDriver.execute(RemoteWebDriver.java:554)org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)atg.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:404)at at org.openqa.selenium . 通过seleniumtest.CaptchaTest.loginTest(CaptchaTest.java:41)的org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)中的$ ByXPath.findElement(By.java:344)在seleniumtest.CaptchaTest.main(CaptchaTest.java:59)引起:org.open qa.selenium.remote.ErrorHandler $ UnknownServerException:无法找到元素:{“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:未知.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)
8 回答
只是为了详细解释以前的答案,CAPTCHA作为“完全自动公共图灵测试告诉计算机和人类分开”的首字母缩写 . 所以,如果“机器”可以解决它,它并不是真正做到的 .
为了解决这个问题,您可以做一些事情 - 使用外部服务的API,例如http://www.deathbycaptcha.com . 您实现他们的API,将CAPTCHA传递给他们并获得文本 . 我观察到的平均解算时间约为10-15秒 .
实施示例(摘自here)
两个问题 .
您有错误的xpath,因此您获得NoSuchElement异常 .
即使你有正确的xpath,你也无法提取文本,因为如果CAPTCHA会破坏这一点
CAPTCHA的全部目的是防止UI自动化!您可能希望使用内部API来验证操作 .
我有一个适用于特定网站的解决方案 . 您可以获取整个页面的快照并获取验证码的图像 . 然后将验证码图像的整个宽度除以总字符数(在验证码中通常它通常是常数) . 现在我们有了验证码图像的各个字符 . 通过重新加载页面来收集验证码的所有可能字符 .
一旦你拥有了所有可能的角色,然后给出任何验证码图像,你可以将它的角色与我们拥有的图像进行比较并决定它是哪个字母或数字 .
要遵循的步骤:
将所有可能的图像保存在文件夹中
现在读取验证码的每个字符图像,并将其与上述文件夹中的所有其他图像进行比较 . 您可以使用像素值比较两个图像public static float getDiff(File f1,File f2,int width,int height)throws IOException {BufferedImage bi1 = null; BufferedImage bi2 = null; bi1 = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB); bi2 = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
具有较少diff值的实际匹配的图像 . 将其名称附加到字符串 .
读取验证码返回字符串1的所有图像后:https://i.stack.imgur.com/FYPhd.png
在上图中,图像名称指定数字或字符 .
这仅适用于像[
1这样的简单验证码
以下是从上图中读取文本的示例代码:
以下是上述程序的输出:
图像源路径:http://2.bp.blogspot.com/-42SgMHAeF8U/Uk8QlYCoy-I/AAAAAAAADSA/TTAVAAgDhio/s1600/love.jpg
从来没有M2使用O,ne喜欢你永远不会说忙碌,一个需要你永远不会欺骗那个让你信任的人,永远不会让那个Zways记得你的人 .
总文字长度:175
忘记密码表单位于iframe中 . 这就是硒没有找到元素的原因 . 您需要先切换到保存表单的iframe,然后运行您的findelement . 你的xpath是正确的 .
使用
driver.switchTo().frame(arg0)
切换到帧 . 见javadoc here为了获得验证码文本,我没有't understand what you meant by '存储测试并比较'. Ideally you shouldn'能够从验证码中读取文本(正如其他人提到的那样) . 我看到的一种替代方法是将captcha值存储为开发和QA环境中的
alt text
. 这样您就可以阅读它并输入文本框 . 当代码进入 生产环境 环境或任何外部环境时,可以删除此alt text
.对于简单的验证码,您可以调用免费的ocr.space online OCR服务 . 只要这只是一个有点扭曲文本的图像就可以了 . 您也可以使用Google Cloud Vision OCR并且它的工作原理相同,但设置起来要复杂一些 .
一个人无法从CAPTCHA读到 . 如果您可以从CAPTCHA阅读,那么使用CAPTCHA是没有意义的 .