org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (323, 140.5) is out of bounds of viewport width (320) and height (282)

作者: admin 分类: Selenium 发布时间: 2019-05-02 15:43  阅读: 225 views

selenium是一个浏览器自动化测试框架。直接驱动浏览器模拟用户操作行为,是一个方便、易用的爬虫首选插件工具。最近在爬取x网的时候,模拟用户操作滑动验证码,一直报错,如下

org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (323, 140.5) is out of bounds of viewport width (320) and height (282)
Build info: version: '3.9.1', revision: '63f7b50', time: '2018-02-07T22:25:02.294Z'
System info: host: 'chenhailongdeMacBook-Pro.local', ip: 'fe80:0:0:0:1c0f:82ba:4ac8:6ff4%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.6', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 62.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 19123, moz:profile: /var/folders/ll/dms152m534b..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: MAC, platformName: MAC, platformVersion: 17.7.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: fc990fdf-77b9-d74b-857d-7687928dcf16
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:160)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
	at org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:667)
	at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:638)
	at org.crawler.spider.webmagic.processor.YoumengPageProcessor.process(YoumengPageProcessor.java:103)
	at us.codecraft.webmagic.Spider.processRequest(Spider.java:416)
	at us.codecraft.webmagic.Spider$1.run(Spider.java:321)
	at us.codecraft.webmagic.thread.CountableThreadPool$1.run(CountableThreadPool.java:74)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

看字面意思的话,是323的宽度超过了原有的320宽度。

修改后的相关代码如下:

WebElement draggable = webDriver.findElement(By.id("nc_1_n1z"));
String css = webDriver.findElement(By.className("nc-lang-cnt")).getCssValue("width");
int width = Integer.parseInt(css.split("px")[0]);
int dragWidth = Integer.parseInt(draggable.getCssValue("width").split("px")[0]);
Actions action = new Actions(webDriver);
action.moveToElement(draggable).perform();
action.clickAndHold(draggable).perform();
dragWidth = width - dragWidth;
				
//总的移动大小是280
Random rd = new Random();
for (int i = 0; i < 50; i++) {
  action.moveByOffset(20, 0);
  action.moveToElement(draggable, 1, 0);
  Thread.sleep(rd.nextInt(10));
}
for (int i = 0; i < 50; i++) {
  action.moveByOffset(20, 0);
  Thread.sleep(rd.nextInt(10));
}
action.build().perform();
Thread.sleep(1000);
webDriver.findElement(By.id("fm-login-submit")).click();

逻辑如下:

a. 先找到整体滑动验证码的div宽度 width

b. 在找到滑块的div宽度 blockWidth

c. 计算出滑块从左边界需要滑动的距离为     width – blockWidth (这里的滑动距离不是从滑块的中心点计算)

d. for循环进行缓慢、匀速滑动

这样的话不会报以上的错误,因为滑动的距离与滑块右边界到达整个滑动验证码的右侧距离复合。

这里需要注意的是d,如果不进行for处理,而是直接滑到右侧,会报以下错误。

2019-04-25 14:56:45,177 ERROR processor.YoumengPageProcessor TypeError: rect is undefined
Build info: version: '3.9.1', revision: '63f7b50', time: '2018-02-07T22:25:02.294Z'
System info: host: 'chenhailongdeMacBook-Pro.local', ip: 'fe80:0:0:0:1c0f:82ba:4ac8:6ff4%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.6', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 62.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 19726, moz:profile: /var/folders/ll/dms152m534b..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: MAC, platformName: MAC, platformVersion: 17.7.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 56b1b266-161b-9740-b311-bbe5b95317f7
org.openqa.selenium.WebDriverException: TypeError: rect is undefined
Build info: version: '3.9.1', revision: '63f7b50', time: '2018-02-07T22:25:02.294Z'
System info: host: 'chenhailongdeMacBook-Pro.local', ip: 'fe80:0:0:0:1c0f:82ba:4ac8:6ff4%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.6', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 62.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 19726, moz:profile: /var/folders/ll/dms152m534b..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: MAC, platformName: MAC, platformVersion: 17.7.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 56b1b266-161b-9740-b311-bbe5b95317f7
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
	at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:160)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
	at org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:667)
	at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:638)
	at org.crawler.spider.webmagic.processor.YoumengPageProcessor.process(YoumengPageProcessor.java:110)
	at us.codecraft.webmagic.Spider.processRequest(Spider.java:416)
	at us.codecraft.webmagic.Spider$1.run(Spider.java:321)
	at us.codecraft.webmagic.thread.CountableThreadPool$1.run(CountableThreadPool.java:74)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

所以通过循环,线程等待的方式模拟用户操作. 但是随着大数据的计算越来越优化。导致程序能够有效识别是人工滑动还是自动滑动。这里的拖动算法还需要进一步优化。(请自行网络搜索,模拟滑动验证码)

 


   原创文章,转载请标明本文链接: org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (323, 140.5) is out of bounds of viewport width (320) and height (282)

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

更多阅读