Firefox - org.openqa.selenium.interactions.MoveTargetOutOfBoundsException

问题描述 投票:1回答:1

我遇到了一个奇怪的情况,在Serenity的页面上我必须滚动到元素:

withAction().moveToElement(webElement).perform();

并且这个方法对于某些元素抛出:

org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: 
(377.375, 958.3999938964844) is out of bounds of viewport width (1268) and height (943)

它只发生在Firefox(Chrome工作正常)。而且几乎所有其他地方,我使用相同的方法都运作良好。所有元素都只是常用元素,如按钮,输入字段等。

有人知道如何解决这个问题吗?

我有:

  • Firefox 61.0.2(64位)
  • Windows 10
  • 宁静1.9.30
  • Geckodriver 0.21.0
java selenium firefox geckodriver serenity-bdd
1个回答
2
投票

此错误消息...

org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: 
(377.375, 958.3999938964844) is out of bounds of viewport width (1268) and height (943)

...暗示Selenium无法专注于所需的元素,因为该元素超出了视口的范围。

你的主要问题是标识为webElement的WebElement超出了Viewport,因此Selenium无法通过moveToElement()方法将焦点转移到所需的元素上。

一个简单的解决方案是使用executeScript()方法在视口中引入所需的元素,然后调用moveToElement()方法,如下所示:

WebElement myElement = driver.findElement(By.xpath("xpath_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", myElement);
withAction().moveToElement(webElement).perform();
© www.soinside.com 2019 - 2024. All rights reserved.