简体   繁体   English

Selenium Webdriver 将鼠标移动到 Point

[英]Selenium Webdriver move mouse to Point

I am currently trying to move the cursor to a point ( org.openqa.selenium.Point ) that has been set by checking for an occurrence of a marker on a live chart from which I can get no details but can find the X and Y coordinates of.我目前正在尝试将光标移动到一个点( org.openqa.selenium.Point ),该点已通过检查实时图表上是否出现标记来设置,从中我无法获得任何详细信息但可以找到 X 和 Y的坐标。

How can I move to the mouse to hover over said point to open the underlying JavaScript menu?如何将鼠标悬停在所述点上以打开底层 JavaScript 菜单?

Current code当前代码

//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);

This doesnt work as Point cannot be cast to org.openqa.selenium.interactions.internal.Coordinates .这不起作用,因为Point无法转换为org.openqa.selenium.interactions.internal.Coordinates

IMHO you should pay your attention to Robot.class恕我直言,你应该注意Robot.class

Still if you want to move the mouse pointer physically, you need to take different approach using 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 provide document coordinates, where as Robot class is based on Screen coordinates, so I have added +120 to compensate the browser header. Webdriver 提供文档坐标,而 Robot 类基于屏幕坐标,因此我添加了 +120 来补偿浏览器标题。
Screen Coordinates : These are coordinates measured from the top left corner of the user's computer screen.屏幕坐标:这些是从用户计算机屏幕的左上角测量的坐标。 You'd rarely get coordinates (0,0) because that is usually outside the browser window.您很少会得到坐标 (0,0),因为它通常在浏览器窗口之外。 About the only time you'd want these coordinates is if you want to position a newly created browser window at the point where the user clicked.您唯一需要这些坐标的情况是,如果您想将新创建的浏览器窗口定位在用户单击的位置。 In all browsers these are in event.screenX and event.screenY .在所有浏览器中,这些都在event.screenXevent.screenY中。
Window Coordinates : These are coordinates measured from the top left corner of the browser's content area.窗口坐标:这些是从浏览器内容区域的左上角开始测量的坐标。 If the window is scrolled, vertically or horizontally, this will be different from the top left corner of the document.如果窗口垂直或水平滚动,这将不同于文档的左上角。 This is rarely what you want.这很少是你想要的。 In all browsers these are in event.clientX and event.clientY.在所有浏览器中,这些都在 event.clientX 和 event.clientY 中。
Document Coordinates : These are coordinates measured from the top left corner of the HTML Document.文档坐标:这些是从 HTML 文档的左上角测量的坐标。 These are the coordinates that you most frequently want, since that is the coordinate system in which the document is defined.这些是您最常需要的坐标,因为这是定义文档的坐标系。

More details you can get here您可以在此处获得更多详细信息

Hope this be helpful to you.希望这对你有所帮助。

Why use java.awt.Robot when org.openqa.selenium.interactions.Actions.class would probably work fine?为什么在org.openqa.selenium.interactions.Actions.class可能正常工作时使用java.awt.Robot Just sayin.只是在说。

Actions builder = new Actions(driver);

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

I am using JavaScript but some of the principles are common I am sure.我正在使用 JavaScript,但我确信其中一些原则是通用的。

The code I am using is as follows:我使用的代码如下:

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

the driver = d . driver = d The location = l is simply {x:300,y:500) - it is just an offset. location = l只是{x:300,y:500) - 它只是一个偏移量。

What I found during my testing was that I could not make it work without using the method to find an existing element first, using that at a basis from where to locate my click.我在测试过程中发现,如果不首先使用该方法查找现有元素,我无法使它工作,并在从哪里找到我的点击的基础上使用它。

I suspect the figures in the locate are a bit more difficult to predict than I thought.我怀疑 locate 中的数字比我想象的更难预测。

It is an old post but this response may help other newcomers like me.这是一篇旧帖子,但此回复可能会帮助像我这样的其他新人。

the solution is implementing anonymous class in this manner:解决方案是以这种方式实现匿名类:

        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);

If you are using a RemoteWebDriver, you can cast WebElement into RemoteWebElement.如果您使用的是 RemoteWebDriver,则可以将 WebElement 转换为 RemoteWebElement。 You can then call getCoordinates() on that object to get the coordinates.然后您可以对该对象调用 getCoordinates() 以获取坐标。

        WebElement el = driver.findElementById("elementId");
        Coordinates c = ((RemoteWebElement)el).getCoordinates();
        driver.getMouse().mouseMove(c);

Got it working with得到它的工作

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

Using MoveToElement you will be able to find or click in whatever point you want, you have just to define the first parameter, it can be the session(winappdriver) or driver(in other ways) which is created when you instance WindowsDriver.使用 MoveToElement 您将能够找到或单击您想要的任何点,您只需定义第一个参数,它可以是会话(winappdriver)或驱动程序(以其他方式),它是在您实例化 WindowsDriver 时创建的。 Otherwise you can set as first parameter a grid (my case), a list, a panel or whatever you want.否则,您可以将网格(我的情况)、列表、面板或任何您想要的设置为第一个参数。

Note: The top-left of your first parameter element will be the position X = 0 and Y = 0注意:第一个参数元素的左上角将是 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();

You can do:你可以做:

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

And then you will get the element.然后你会得到元素。 you can add .click() to click on the element.您可以添加.click()以单击该元素。

The executeScript allow you to get the document element and use elementFromPoint built-in javascript function to click by X,Y Cord executeScript允许您获取文档元素并使用elementFromPoint内置的 javascript 函数通过 X,Y Cord 单击

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

Rotbot is good solution.机器人是很好的解决方案。 It works for me.这个对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM