首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Selenium MoveTargetOutOfBoundsException即使在滚动到元素之后

Selenium MoveTargetOutOfBoundsException即使在滚动到元素之后
EN

Stack Overflow用户
提问于 2019-06-22 22:13:14
回答 2查看 6.2K关注 0票数 2
代码语言:javascript
运行
复制
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

我正在犯错误:

代码语言:javascript
运行
复制
org.openqa.selenium.interactions.MoveTargetOutOfBoundsException

有人建议我:

随着Chromedriver切换到版本75中的w3c遵从性,在对其使用操作之前,您必须滚动到视图中的任何元素

所以我添加了这个代码(isVisibleInViewport来自here)

代码语言:javascript
运行
复制
  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();
}

然而,我仍然收到同样的错误:

代码语言:javascript
运行
复制
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}

那么问题是什么呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-22 14:43:03

我不确定您是否面临同样的问题,但当我将色度驱动器更新为version75时,我也遇到了类似的问题。

在我使用来自左上角的偏移之前,它在以前版本的色度驱动器上运行得很好。

但是从现在开始(Version75),我必须使用来自web元素的中心的偏移。

从web元素的中心计算坐标解决了我的问题。

票数 3
EN

Stack Overflow用户

发布于 2020-08-14 22:05:21

下面这些对我来说很管用。

代码语言:javascript
运行
复制
WebElement element = driver.findElement(locator);

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

最初张贴在这里:

https://github.com/mozilla/geckodriver/issues/776#issuecomment-353595601

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56719535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档