问题:Firefox - org.openqa.selenium.interactions.MoveTargetOutOfBoundsException

我遇到了一个奇怪的情况,在 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位)

  • 视窗 10

  • 宁静 1.9.30

  • 壁虎驱动 0.21.0

解答

此错误消息...

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

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

您的主要问题是标识为 webElementWebElement 不在视口中,因此 Selenium 无法通过moveToElement()方法将 focus 移动到所需元素上。

解决方案

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

WebElement myElement = driver.findElement(By.xpath("xpath_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", myElement);
withAction().moveToElement(webElement).perform();
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐