问题
I have faced a weird situation, where on the page in Serenity I have to scroll to the element:
withAction().moveToElement(webElement).perform();
and this method for some elements throws:
org.openqa.selenium.interactions.MoveTargetOutOfBoundsException:
(377.375, 958.3999938964844) is out of bounds of viewport width (1268) and height (943)
It happens only in Firefox(Chrome works fine). Moreover almost all other places, where I'm using the same method are working well. All elements are just usual elements like buttons, input fields, etc.
Does anybody know how to fix this in Firefox?
I have:
- Firefox 61.0.2 (64-bit)
- Windows 10
- Serenity 1.9.30
- Geckodriver 0.21.0
回答1:
This error message...
org.openqa.selenium.interactions.MoveTargetOutOfBoundsException:
(377.375, 958.3999938964844) is out of bounds of viewport width (1268) and height (943)
...implies that Selenium was unable to focus on the desired element as the element was out of bounds of the viewport.
Your main issue is the WebElement identified as webElement is out of Viewport so Selenium can't move the focus on the desired element through moveToElement()
method.
Solution
A simple solution would be to use the executeScript()
method to bring the desired element within the viewport and then invoke moveToElement()
method as follows:
WebElement myElement = driver.findElement(By.xpath("xpath_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", myElement);
withAction().moveToElement(webElement).perform();
来源:https://stackoverflow.com/questions/52116195/firefox-org-openqa-selenium-interactions-movetargetoutofboundsexception