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
?
首先,在调用GeckoDriver时,第一组日志确认方言正在W3C
,如下所示:
org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
你是对的,因为Java DocsformoveToElement()
仍然提到:
public Actions moveToElement(WebElement target, int xOffset, int yOffset)
Description:
Moves the mouse to an offset from the top-left corner of the element. The element is scrolled into view and its location is calculated using getBoundingClientRect.
Parameters:
target - element to move to.
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.
Returns:
A self reference.
我能够重现您的问题,如下所示:
代码块:
new Actions(driver).moveToElement(element,0,0).doubleClick().build().perform();
跟踪日志:
Oct 16, 2018 6:06:13 PM org.openqa.selenium.interactions.Actions moveToElement INFO: When using the W3C Action commands, offsets are from the center of element 1539693373141 webdriver::server DEBUG -> POST /session/180ab0f0-21a3-4e38-8c92-d208fac77827/actions {"actions":[{"id":"default mouse","type":"pointer","parameters":{"pointerType":"mouse"},"actions":[{"duration":100,"x":0,"y":0,"type":"pointerMove","origin":{"ELEMENT":"774efad2-7ee0-40a3-bcaa-3a4d5fcff47b","element-6066-11e4-a52e-4f735466cecf":"774efad2-7ee0-40a3-bcaa-3a4d5fcff47b"}},{"button":0,"type":"pointerDown"},{"button":0,"type":"pointerUp"},{"button":0,"type":"pointerDown"},{"button":0,"type":"pointerUp"}]}]} 1539693373166 Marionette TRACE 0 -> [0,5,"WebDriver:PerformActions",{"actions":[{"actions":[{"duration":100,"origin":{"element-6066-11e4-a52e-4f735466cecf":"774e ... pointerDown"},{"button":0,"type":"pointerUp"}],"id":"default mouse","parameters":{"pointerType":"mouse"},"type":"pointer"}]}]
正如@Andreas在讨论中指出的那样,偏移量来自元素的中心,而不是从左上角@FlorentB。
根据 JsonWireProtocol 规范中的/session/:sessionId/moveto,它被提及:
/session/:sessionId/moveto POST /session/:sessionId/moveto Move the mouse by an offset of the specificed element. If no element is specified, the move is relative to the current mouse cursor. If an element is provided but no offset, the mouse will be moved to the center of the element. If the element is not visible, it will be scrolled into view. URL Parameters: :sessionId - ID of the session to route the command to. JSON Parameters: element - {string} Opaque ID assigned to the element to move to, as described in the WebElement JSON Object. If not specified or is null, the offset is relative to current position of the mouse. xoffset - {number} X offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element. yoffset - {number} Y offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
根据 WebDriver W3C 编辑器草稿中的指针操作部分
,提到:表示 Web 元素的对象
让元素等于尝试获取具有参数源的已知连接元素的结果。
设 x 元素和 y 元素是计算元素的视图中心点的结果。
设 x 等于 x 元素 + x 偏移量,y 等于 y 元素 + y 偏移量。
视野内中心点
元素的视图内中心点是矩形的原点位置,矩形是元素的第一个 DOM 客户端矩形与初始视区之间的交点。给定已知在视图中的元素,其计算公式为:
- 让矩形成为通过对元素调用 getClientRects 返回的 DOMRect 序列的第一个元素。
- 设左为 (max(0, min(x 坐标, x 坐标 + 宽度维度(((。
- 设右边是(min(innerWidth, max(x coordinate, x 坐标 + width dimension(((。
- 设 top 为 (max(0, min(y 坐标, y 坐标 + 高度维度(((。
- 设底部为 (min(innerHeight, max(y 坐标, y 坐标 + 高度维度(((。
- 设 x 为 (0.5 × (左 + 右((。
- 设 y 为 (0.5 ×(顶部 + 底部((。
- 将 x 和 y 作为一对返回。
因此可以得出结论,偏移量来自中心,但Jave 文档尚未更新。
如果要求是单击元素的左上角,那么以下代码片段就可以了
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
Actions moveAndClick = new
int yOffset=element.getRect().height/-2;//top
int xOffset=element.getRect().width/-2;//left
Actions(driver).moveToElement(element,xOffset,yOffset).doubleClick().perform();