Selenium Webdriver 将鼠标移动到 Point



我目前正在尝试将光标移动到一个点(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

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

恕我直言,您应该注意机器人.class

仍然,如果您想物理移动鼠标指针,则需要使用机器人类采取不同的方法

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 文档的左上角测量的坐标。这些是您最常需要的坐标,因为这是定义文档的坐标系。

您可以在此处获得更多详细信息

希望这对您有所帮助。

我正在使用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)- 它只是一个偏移量。

我在测试过程中发现,如果不使用该方法先找到现有元素,我就无法使其工作,并在定位单击的位置的基础上使用它。

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

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

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

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

如果您使用的是 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)");

然后你会得到元素。 您可以添加.click()以单击该元素。

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

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

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

最新更新