Selenium Webdriver移动鼠标到指定位置

17

我目前正在尝试将光标移动到一个已被检测到的点(org.openqa.selenium.Point),这个点是从实时图表中找到的,虽然我无法获取有关它的详细信息,但可以找到其X和Y坐标。

我该如何将鼠标移动到该点上,以打开底层的JavaScript菜单?

当前代码

//finds marker on the current web page

Point image = page.findImage("C:\\Pictures\\marker.png") ;

//move mouse to this x,y location 

driver.getMouse().mouseMove((Coordinates) image);

这样不起作用,因为Point无法转换为org.openqa.selenium.interactions.internal.Coordinates


如果您接受仅使用JavaScript的解决方案,则可能可以从此基于JavaScript的拖放和鼠标移动文章中适应代码:http://ynot408.wordpress.com/2011/09/22/drag-and-drop-using-selenium-webdriver - David
9个回答

15

org.openqa.selenium.interactions.Actions.class 可以正常工作时,为什么要使用 java.awt.Robot 呢?只是说一下。

Actions builder = new Actions(driver);

builder.keyDown(Keys.CONTROL)
   .click(someElement)
   .moveByOffset( 10, 25 );
   .click(someOtherElement)
   .keyUp(Keys.CONTROL).build().perform();

这里缺少 .perform() 以使其生效。 - Alvaro Mendez

15

我认为你应该注意 Robot.class

如果您仍然想要手动移动鼠标指针,则需要使用Robot类采取不同的方法

  Point coordinates = driver.findElement(By.id("ctl00_portalmaster_txtUserName")).getLocation();
  Robot robot = new Robot();
  robot.mouseMove(coordinates.getX(),coordinates.getY()+120);
Webdriver提供文档坐标,而Robot类基于屏幕坐标,因此我添加了+120来补偿浏览器标题栏。
屏幕坐标:这些是从用户计算机屏幕左上角开始测量的坐标。你很少会得到坐标(0,0),因为通常它在浏览器窗口之外。你需要这些坐标的唯一时候是如果你想要将新创建的浏览器窗口定位在用户单击的点上。
在所有浏览器中,可以使用event.screenXevent.screenY获得这些坐标。
窗口坐标:这些坐标是从浏览器内容区域的左上角开始测量的。如果窗口被垂直或水平滚动,这将与文档的左上角不同。这通常不是你想要的。
在所有浏览器中,可以使用event.clientXevent.clientY获取这些坐标。
文档坐标:这些坐标是从HTML文档的左上角开始测量的。这是你最常需要的坐标系,因为这是定义文档的坐标系。
更多详细信息请参考这里
希望对你有所帮助。

2
不要这样做,它只能在您的本地计算机上运行。如果您的测试代码连接到Selenium网格,它将无法工作,因为此代码将在您的本地计算机上移动光标,而不是在远程网格计算机上移动。 - Ardesco

2

我正在使用JavaScript,但我相信其中一些原则是通用的。

我使用的代码如下:

    var s = new webdriver.ActionSequence(d);
    d.findElement(By.className('fc-time')).then(function(result){
        s.mouseMove(result,l).click().perform();
    });

driver = d。这里的location = l只是一个偏移量,它的值是{x:300,y:500)

在我的测试中,我发现如果不使用查找现有元素的方法先找到一个基础元素,就无法使其正常工作,然后以此为基础定位我的点击位置。

我怀疑定位中的数字比我想象的要更难预测。

虽然这是一篇旧帖子,但我的回答可能会对其他像我一样的新手有所帮助。


0

使用MoveToElement,您将能够找到或单击您想要的任何点,您只需要定义第一个参数,它可以是会话(winappdriver)或驱动程序(其他方式)在实例化WindowsDriver时创建。否则,您可以将网格(我的情况),列表,面板或任何您想要的设置为第一个参数。

注意:第一个参数元素的左上角将是位置X = 0和Y = 0

   Actions actions = new Actions(this.session);
    int xPosition = this.session.FindElementsByAccessibilityId("GraphicView")[0].Size.Width - 530;
    int yPosition = this.session.FindElementsByAccessibilityId("GraphicView")[0].Size.Height- 150;
    actions.MoveToElement(this.xecuteClientSession.FindElementsByAccessibilityId("GraphicView")[0], xPosition, yPosition).ContextClick().Build().Perform();

0

已经解决了,使用

Actions builder = new Actions(driver);
WebElement el = some element;
builder.keyDown(Keys.CONTROL)
.moveByOffset( 10, 25 )
.clickAndHold(el)
.build().perform();

0

解决方案是以这种方式实现匿名类:

        import org.openqa.selenium.Point;
        import org.openqa.selenium.interactions.HasInputDevices;
        import org.openqa.selenium.interactions.Mouse;
        import org.openqa.selenium.interactions.internal.Coordinates;

        .....

        final Point image = page.findImage("C:\\Pictures\\marker.png") ;

        Mouse mouse = ((HasInputDevices) driver).getMouse();

        Coordinates imageCoordinates =  new Coordinates() {

              public Point onScreen() {
                throw new UnsupportedOperationException("Not supported yet.");
              }

              public Point inViewPort() {
                Response response = execute(DriverCommand.GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW,
        ImmutableMap.of("id", getId()));

    @SuppressWarnings("unchecked")
    Map<String, Number> mapped = (Map<String, Number>) response.getValue();

    return new Point(mapped.get("x").intValue(), mapped.get("y").intValue());
              }

              public Point onPage() {
                return image;
              }

              public Object getAuxiliary() {
                // extract the selenium imageElement id (imageElement.toString() and parse out the "{sdafbsdkjfh}" format id) and return it
              }
            };

        mouse.mouseMove(imageCoordinates);

0
如果您正在使用RemoteWebDriver,可以将WebElement强制转换为RemoteWebElement。然后,您可以在该对象上调用getCoordinates()以获取坐标。
        WebElement el = driver.findElementById("elementId");
        Coordinates c = ((RemoteWebElement)el).getCoordinates();
        driver.getMouse().mouseMove(c);

0

你可以做:

JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("document.elementFromPoint(25,25)");

然后您将获得该元素。您可以添加.click()来单击该元素。

executeScript允许您获取文档元素并使用内置的JavaScript函数elementFromPoint通过X、Y坐标进行单击。


-1
Robot robot = new Robot();
robot.mouseMove(coordinates.x,coordinates.y+80);

Rotbot是一个很好的解决方案。 它对我很有效。


1
无法与网格一起使用,或者如果有东西在Selenium浏览器窗口前面也会无法工作。这是一个非常糟糕的答案。 - Ardesco

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接