我遇到了一个奇怪的情况,在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工作正常)。而且几乎所有其他地方,我使用相同的方法都运作良好。所有元素都只是常用元素,如按钮,输入字段等。
有人知道如何解决这个问题吗?
我有:
此错误消息...
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();