Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
998 views
in Technique[技术] by (71.8m points)

java - Firefox - org.openqa.selenium.interactions.MoveTargetOutOfBoundsException

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...