new Actions(data).moveToElement(element,x,y).perform();
此代码使用Selenium 3.8.1,Chrome 63,chromeDriver2.3.8
最后升级到: Selenium 3.14,Chrome 75,chromedriver 75.0.3770.9
我正在犯错误:
org.openqa.selenium.interactions.MoveTargetOutOfBoundsException
有人建议我:
随着Chromedriver切换到版本75中的w3c遵从性,在对其使用操作之前,您必须滚动到视图中的任何元素
所以我添加了这个代码(isVisibleInViewport来自here)
private boolean isVisibleInViewport(WebElement element) {
return (boolean)((JavascriptExecutor)data).executeScript(
"var elem = arguments[0], " +
" box = elem.getBoundingClientRect(), " +
" cx = box.left + box.width / 2, " +
" cy = box.top + box.height / 2, " +
" e = document.elementFromPoint(cx, cy); " +
"for (; e; e = e.parentElement) { " +
" if (e === elem) " +
" return true; " +
"} " +
"return false; "
, element);
}
public void moveToElement(WebElement element, int x,int y){
if (!isVisibleInViewport(element)) ((JavascriptExecutor) data).executeScript("arguments[0].scrollIntoView();", element);
new Actions(data).moveToElement(element,x,y).perform();
}
然而,我仍然收到同样的错误:
org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: move target out of bounds
(Session info: chrome=75.0.3770.100)
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
System info: host: 'TEST_VIRTUAL_114', ip: '192.168.215.2', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.90 (a6dcaf7e3ec6f..., userDataDir: C:\Users\TestVirtualPC\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:63560}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
那么问题是什么呢?
发布于 2019-07-22 14:43:03
我不确定您是否面临同样的问题,但当我将色度驱动器更新为version75时,我也遇到了类似的问题。
在我使用来自左上角的偏移之前,它在以前版本的色度驱动器上运行得很好。
但是从现在开始(Version75),我必须使用来自web元素的中心的偏移。
从web元素的中心计算坐标解决了我的问题。
发布于 2020-08-14 22:05:21
下面这些对我来说很管用。
WebElement element = driver.findElement(locator);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
最初张贴在这里:
https://github.com/mozilla/geckodriver/issues/776#issuecomment-353595601
https://stackoverflow.com/questions/56719535
复制相似问题