Java Selenium Webdriver将鼠标移动到点

Java Selenium Webdriver将鼠标移动到点,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我目前正试图将光标移动到一个点(org.openqa.selenium.point),该点是通过检查活动图表上是否出现标记而设置的,我无法从中获得详细信息,但可以找到活动图表的X和Y坐标 如何移动鼠标悬停在所述点上以打开底层JavaScript菜单 现行代码 这不起作用,因为点不能强制转换到org.openqa.selenium.interactions.internal.Coordinates您应该注意的地方 但是,如果您想实际移动鼠标指针,则需要使用Robot类采取不同的方法 Webdriv

我目前正试图将光标移动到一个点(
org.openqa.selenium.point
),该点是通过检查活动图表上是否出现标记而设置的,我无法从中获得详细信息,但可以找到活动图表的X和Y坐标

如何移动鼠标悬停在所述点上以打开底层JavaScript菜单

现行代码
这不起作用,因为
不能强制转换到
org.openqa.selenium.interactions.internal.Coordinates

您应该注意的地方

但是,如果您想实际移动鼠标指针,则需要使用Robot类采取不同的方法

Webdriver提供文档坐标,其中as Robot类基于屏幕坐标,因此我添加了+120以补偿浏览器标题。
屏幕坐标:这些是从用户计算机屏幕左上角测量的坐标。您很少会获得坐标(0,0),因为它通常位于浏览器窗口之外。如果您想将新创建的浏览器窗口定位在用户单击的位置,则您可能只需要这些坐标。 在所有浏览器中,它们都位于
event.screenX
event.screenY

窗口坐标:这些坐标是从浏览器内容区域的左上角测量的。如果垂直或水平滚动窗口,这将不同于文档的左上角。这很少是你想要的。 在所有浏览器中,它们都位于event.clientX和event.clientY中。
文档坐标:这些坐标是从HTML文档的左上角测量的。这些是您最常用的坐标,因为这是定义文档的坐标系

您可以获得更多详细信息

希望这对你有帮助

Robot robot = new Robot();
robot.mouseMove(coordinates.x,coordinates.y+80);
Rotbot是一个很好的解决方案。
它适合我。

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

        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);
import org.openqa.selenium.Point;
导入org.openqa.selenium.interactions.HasInputDevices;
导入org.openqa.selenium.interactions.Mouse;
导入org.openqa.selenium.interactions.internal.Coordinates;
.....
最终点图像=page.findImage(“C:\\Pictures\\marker.png”);
鼠标=((HasInputDevices)驱动程序).getMouse();
坐标imageCoordinates=新坐标(){
屏幕上的公共点(){
抛出新的UnsupportedOperationException(“尚未支持”);
}
公共点输入端口(){
Response Response=execute(DriverCommand.GET元素位置)一旦滚动到视图中,
ImmutableMap.of(“id”,getId());
@抑制警告(“未选中”)
Map mapped=(Map)response.getValue();
返回新点(mapped.get(“x”).intValue(),mapped.get(“y”).intValue());
}
第()页上的公共点{
返回图像;
}
公共对象getAuxiliary(){
//提取selenium imageElement id(imageElement.toString()并解析出“{sdafbsdkjfh}”格式id)并返回它
}
};
mouse.mouseMove(图像坐标);
既然java.awt.Robot可能工作正常,为什么还要使用它?只是说说而已

Actions builder = new Actions(driver);

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

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

我使用的代码如下:

    var s = new webdriver.ActionSequence(d);
    d.findElement(By.className('fc-time')).then(function(result){
        s.mouseMove(result,l).click().perform();
    });
驱动程序=d
location=l
只是
{x:300,y:500)
——它只是一个偏移量

我在测试过程中发现,如果不先使用该方法查找现有元素,并在从何处定位单击的基础上使用该方法,我将无法使其正常工作

我怀疑地图上的数字比我想象的更难预测


这是一篇老文章,但此响应可能会帮助像我这样的其他新手。

如果您使用RemoteWebDriver,您可以将WebElement强制转换为RemoteWebElement。然后您可以对该对象调用getCoordinates()以获取坐标

        WebElement el = driver.findElementById("elementId");
        Coordinates c = ((RemoteWebElement)el).getCoordinates();
        driver.getMouse().mouseMove(c);
让它工作起来

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

使用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();
你可以做:

JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("document.elementFromPoint(25,25)");
然后您将获得该元素。您可以添加
。单击()
以单击该元素


executeScript
允许您获取文档元素,并使用
elementFromPoint
内置javascript函数单击X,Y线

如果您对纯javascript解决方案满意,则可能能够在基于javascript的拖放和鼠标移动上修改本文中的代码:不要这样做,它只会在本地machine。如果您的测试代码连接到selenium网格,它将不工作,因为此代码将在本地计算机上而不是远程网格计算机上移动光标。将不工作于网格,或者如果selenium浏览器窗口前面有东西。这是一个非常糟糕的答案。缺少.perform()最后,让它发生。
   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();
JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("document.elementFromPoint(25,25)");