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

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

我无法从图像中读取文本.我要提一下代码和输出..

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

}

输出:错误

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:

07000 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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Selenium可以用于解决图片验证码的问题。一般来说,可以通过以下步骤来实现: 1. 使用Selenium打开网页并输入用户名和密码。 2. 使用Selenium获取验证码图片的URL,并下载图片到本地。 3. 使用Python的第三方库(如Pillow)读取图片,并对图片进行处理,以便于后续的识别。 4. 使用Python的第三方库(如Tesseract)对图片进行OCR识别,得到验证码文本。 5. 使用Selenium输入验证码并提交表单。 下面是一个示例代码,用于解决滑块验证码的问题: ```python from selenium import webdriver from PIL import Image import time # 设置Chrome驱动路径 chrome_path = r"C:\Users\11248\AppData\Local\Google\Chrome\Application\chromedriver.exe" # 打开网页 url = 'https://www.xxx.com/login' driver = webdriver.Chrome(chrome_path) driver.get(url) # 输入用户名和密码 username = driver.find_element_by_id('username') password = driver.find_element_by_id('password') username.send_keys('your_username') password.send_keys('your_password') # 获取验证码图片的URL,并下载图片到本地 img_url = driver.find_element_by_id('captcha').get_attribute('src') driver.get_screenshot_as_file('screenshot.png') img = Image.open('screenshot.png') left = driver.find_element_by_id('captcha').location['x'] top = driver.find_element_by_id('captcha').location['y'] right = left + driver.find_element_by_id('captcha').size['width'] bottom = top + driver.find_element_by_id('captcha').size['height'] img.crop((left, top, right, bottom)).save('captcha.png') # 对图片进行处理,以便于后续的识别 captcha = Image.open('captcha.png') captcha = captcha.convert('L') captcha = captcha.point(lambda x: 0 if x < 150 else 255) # 对图片进行OCR识别,得到验证码文本 text = pytesseract.image_to_string(captcha, lang='eng', config='--psm 7') print('验证码为:', text) # 输入验证码并提交表单 captcha_input = driver.find_element_by_id('captcha_input') captcha_input.send_keys(text) submit_button = driver.find_element_by_id('submit_button') submit_button.click() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值