首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以以编程方式确定是否使用W3C操作命令?

是否可以以编程方式确定是否使用W3C操作命令?
EN

Stack Overflow用户
提问于 2018-08-22 20:27:34
回答 2查看 2.8K关注 0票数 4

硒Javadoc for Actions.moveToElement表示xOffsetyOffset参数的含义如下。

代码语言:javascript
复制
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。

代码语言:javascript
复制
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来记录单击的位置)。此外,在运行程序的终端中输出以下消息。

代码语言:javascript
复制
org.openqa.selenium.interactions.Actions moveToElement
INFO: When using the W3C Action commands, offsets are from the center of element

是否可以以编程方式确定偏移量是来自Actions.moveToElement**?**元素的中间还是左上角?

EN

回答 2

Stack Overflow用户

发布于 2018-10-16 13:14:02

首先,当调用W3C时,第一组日志确认方言,如下所示:

代码语言:javascript
复制
org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C

您是正确的,因为JavaDocsformoveToElement()仍然提到:

代码语言:javascript
复制
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.

我可以将你的问题转载如下:

  • 代码块: 新Actions(driver).moveToElement(element,0,0).doubleClick().build().perform();
  • 跟踪日志: 2018年10月16日6:06:13 PM org.openqa.selenium.interactions.Actions moveToElement信息:当使用W3C操作命令时,偏移量来自元素1539693373141的中心::服务器调试-> POST ->{“actions”:[{“id”:“默认鼠标”,“键入”:“指针”,“参数”:{“pointerType”:“鼠标”},“操作”:{“持续时间”:100,"x":0,"y":0,“键入”:“pointerMove”,“pointerMove”,“键入”:0,“键入”:“pointerDown”},{“按钮”:0,“键入”:“pointerUp”},{“按钮”:0,“键入”:“pointerDown”},{“按钮”:0,“类型”:“pointerUp”} 1539693373166 pointerUp跟踪0 -> [0,5,"WebDriver:PerformActions",{“操作”:[{“操作”:{“持续时间”:100,->.pointerDown"},{“按钮”:0,“类型”:“pointerUp”},“id”:“默认鼠标”,“参数”:{“pointerType”:“鼠标”},“类型”:“指针”}}]

正如@Andreas在讨论中指出的,偏移是从元素的中心,而不是从左上角。 @FlorentB。明确指出:

  • 正如/session/:sessionId/movetoJsonWireProtocol规范中提到的那样: /session/:sessionId/moveto /session/:sessionId/moveto通过特定元素的偏移量移动鼠标。如果未指定任何元素,则移动相对于当前鼠标光标。如果提供了一个元素,但没有偏移,鼠标将被移动到元素的中心。如果元素不可见,它将被滚动到视图中。URL参数::要将命令路由到的会话的会话ID。JSON参数:分配给要移动到的元素的元素{string}不透明ID,如WebElement JSON对象中所描述的。如果未指定或为null,则偏移量相对于鼠标的当前位置。相对于元素的左上角,x偏移量- {number} x偏移量要移动到.如果没有指定,鼠标将移动到元素的中间。Y偏移量- {number} Y偏移量相对于元素的左上角移动.如果没有指定,鼠标将移动到元素的中间。
  • 根据指针动作WebDriver W3C编辑器草稿中的部分,可以提到: 表示web元素的对象。
代码语言:javascript
复制
1. Let element be equal to the result of trying to get a known connected element with argument origin.
2. Let x element and y element be the result of calculating the in-view center point of element.
3. Let x equal x element + x offset, and y equal y element + y offset.

视点中心点

元素的视图中心点是矩形的起始位置,即元素的第一个DOM客户端矩形与初始视图端口之间的交集。给定已知的视图元素,计算为:

  • 让矩形是通过调用元素上的DOMRect序列返回的getClientRects序列的第一个元素。
  • 设左be (max(0,min(x坐标,x坐标+宽度维))。
  • 设右为(最小(innerWidth,max(x坐标,x坐标+宽度维))。
  • 设顶部为(最大值(0,min(y坐标,y坐标+高度维)。
  • 设底部be (最小(innerHeight,max(y坐标,y坐标+高度维))。
  • 设x为(0.5×(左+右))。
  • 设y为(0.5×(顶部+底部))。
  • 将x和y作为对返回。

因此可以得出结论,偏移量来自center,但Jave尚未更新。

票数 1
EN

Stack Overflow用户

发布于 2020-07-10 07:46:12

如果问题是单击元素的左上角,那么下面的代码片段就可以了。

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51974620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档