【问题标题】:Selenium MoveTargetOutOfBoundsException even after scrolling to elementSelenium MoveTargetOutOfBoundsException 即使在滚动到元素之后
【发布时间】:2019-11-05 06:07:16
【问题描述】:
new Actions(data).moveToElement(element,x,y).perform();

此代码适用于:Selenium 3.8.1、Chrome 63、chromedriver 2.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}

那么问题出在哪里?

【问题讨论】:

标签: java selenium


【解决方案1】:

我不确定您是否遇到同样的问题,但是当我将我的 chromedriver 更新到 version75 时遇到了类似的问题。

在我使用左上角的偏移量之前,它在以前版本的 chromedriver 中运行良好。

但是从现在开始(版本 75)我必须使用 从 web 元素的中心偏移

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

【讨论】:

    【解决方案2】:

    以下内容对我有用。

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

    原贴在这里:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      相关资源
      最近更新 更多