用于Actions.moveToElement的硒Javadoc 表示xOffset和yOffset参数的含义如下.
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.请考虑以下程序,该程序可在Linux上针对Firefox Quantum运行.
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("www.google"); 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来记录点击).此外,在运行程序的终端中将输出以下消息.
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是否可以以编程方式确定偏移量是相对于Actions.moveToElement元素的中心还是左上角?
Is it possible to programmatically determine whether the offsets are from the center or the top-left corner of the element for Actions.moveToElement?
推荐答案首先,调用 GeckoDriver 时,第一组日志确认方言为 W3C 如下:
To start with, when invoking GeckoDriver the first set of logs confirms that dialect being W3C as follows:
org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C您作为Java文档是正确的.html#moveToElement-org.openqa.selenium.WebElement-int-int-"rel =" nofollow noreferrer> moveToElement() 仍然提到:
You are correct as the Java Docs for moveToElement() still mentions that:
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.我可以按照以下步骤重现您的问题:
I am able to reproduce your issue as follows:
-
代码块:
Code Block:
跟踪日志:
Trace Logs:
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.明确指出:
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:
-
按照/session/:sessionId/moveto 提到了 JsonWireProtocol 规范:
按照指针操作 WebDriver W3C编辑器草案中的部分提到:
As per the Pointer Actions section within WebDriver W3C Editor's Draft it is mentioned that:
代表网络元素的对象
让元素等于尝试获取自变量来源的已知连接元素的结果.
Let element be equal to the result of trying to get a known connected element with argument origin.
让x元素和y元素成为计算元素的视图内中心点的结果.
Let x element and y element be the result of calculating the in-view center point of element.
让x等于x元素+ x偏移量,并且y等于y元素+ y偏移量.
Let x equal x element + x offset, and y equal y element + y offset.
元素的视图中心点是矩形的原点位置,该矩形是元素的第一个DOM客户矩形和初始视口之间的交点.给定已知可见的元素,其计算公式为:
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:
- 让矩形成为通过调用元素上的getClientRects返回的DOMRect序列的第一个元素.
- 让左为(max(0,min(x坐标,x坐标+宽度尺寸))).
- 让我们成为(min(innerWidth,max(x坐标,x坐标+宽度尺寸))).
- 让top成为(max(0,min(y坐标,y坐标+高度尺寸))).
- 让底部为(min(innerHeight,max(y坐标,y坐标+高度尺寸)))).
- 让x为(0.5×(左+右)).
- 让我们成为(0.5×(顶部+底部)).
- 将x和y成对返回.
因此可以得出结论,偏移量来自中心,但是 Jave Docs 尚未更新.
So it is can be concluded that the offsets are from the center but the Jave Docs are yet to be updated.
更多推荐
是否可以通过编程确定是否使用W3C动作命令?
发布评论