Selenium Javadoc forActions.moveToElement
表明xOffset
和参数的含义yOffset
如下。
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.
考虑以下程序,在 Linux 上针对 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();
}
}
运行以下程序时,双击发生在搜索框的中间而不是左上角(我知道这是因为我注入了 JS 来记录单击的位置)。此外,在运行程序的终端中会输出以下消息。
org.openqa.selenium.interactions.Actions moveToElement
INFO: When using the W3C Action commands, offsets are from the center of element
是否可以以编程方式确定偏移量是来自元素的中心还是左上角Actions.moveToElement
?