Selenium Webdriver将鼠标移动到Point

我目前正在尝试将光标移动到一个点( org.openqa.selenium.Point ),该点是通过检查实时图表上标记的出现而设置的,我从中可以得到没有细节但可以找到X和Y的坐标。

如何移动鼠标hover在所述点以打开基础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

恕我直言,你应该注意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.clientX和event.clientY中。
文档坐标 :这些坐标是从HTML文档的左上角开始测量的。 这些是您最常需要的坐标,因为这是定义文档的坐标系。

您可以在这里获得更多细节

希望这对你有所帮助。

为什么在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(); 

我正在使用JavaScript,但我确信其中一些原则很常见。

我使用的代码如下:

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

driver = dlocation = l只是{x:300,y:500) – 它只是一个偏移量。

我在测试期间发现的是,如果不首先使用该方法查找现有元素,我将无法使其工作,在此基础上使用该元素来定位我的点击。

我怀疑定位中的数字比我想象的要难得多。

这是一个老post,但这个回应可能会帮助像我这样的其他新人。

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

  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 mapped = (Map) 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); 

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

  WebElement el = driver.findElementById("elementId"); Coordinates c = ((RemoteWebElement)el).getCoordinates(); driver.getMouse().mouseMove(c); 
 Robot robot = new Robot(); robot.mouseMove(coordinates.x,coordinates.y+80); 

Rotbot是很好的解决方案。 这个对我有用。