The Selenium Javadoc for Actions.moveToElement
indicate that the meanings of the xOffset
and yOffset
arguments are as follows.
xOffset - Offset from the top-left corner. A negative value means coordinates left from the element.
yOffset - Offset from the top-left corner. A negative value means coordinates above the element.
Consider the following program, run on Linux against Firefox Quantum.
public class FirefoxTest {
public static void main(String[] args) {
// Set up driver
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("http://www.google.com");
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
// Perform a move and click action to see where it lands.
Actions moveAndClick = new Actions(driver).moveToElement(element,0,0).doubleClick();
moveAndClick.perform();
}
}
When the following program is run, the double-click occurs in the middle of the search box rather than at the top left corner (I know this because I injected JS to log the location of the click). Moreover, the following message is output in the terminal where the program is run.
org.openqa.selenium.interactions.Actions moveToElement
INFO: When using the W3C Action commands, offsets are from the center of element
Is it possible to programmatically determine whether the offsets are from the center or the top-left corner of the element for Actions.moveToElement
?
To start with, when invoking GeckoDriver the first set of logs confirms that dialect being
W3C
as follows:You are correct as the Java Docs for
moveToElement()
still mentions that:I am able to reproduce your issue as follows:
Code Block:
Trace Logs:
As @Andreas pointed out in the discussion offsets are from the center of element instead of from the top-left corner @FlorentB. clearly pointed that:
As per /session/:sessionId/moveto in JsonWireProtocol specification it was mentioned:
As per the Pointer Actions section within WebDriver W3C Editor's Draft it is mentioned that:
An object that represents a web element
Let element be equal to the result of trying to get a known connected element with argument origin.
Let x element and y element be the result of calculating the in-view center point of element.
Let x equal x element + x offset, and y equal y element + y offset.
In-view center point
An element’s in-view center point is the origin position of the rectangle that is the intersection between the element’s first DOM client rectangle and the initial viewport. Given an element that is known to be in view, it is calculated as:
So it is can be concluded that the offsets are from the center but the Jave Docs are yet to be updated.