Selenium Webdriver将鼠标移动到Point

AFo*_*ley 16 java selenium selenium-webdriver

我目前正在尝试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);
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为Point无法投射org.openqa.selenium.interactions.internal.Coordinates.

eug*_*kov 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);
Run Code Online (Sandbox Code Playgroud)

Webdriver提供文档坐标,其中Robot类基于Screen坐标,因此我添加了+120来补偿浏览器标题.
屏幕坐标:这些坐标是从用户计算机屏幕的左上角开始测量的.你很少得到坐标(0,0)因为它通常在浏览器窗口之外.关于您想要这些坐标的唯一时间是您希望将新创建的浏览器窗口定位在用户单击的位置.在所有浏览器中,这些都在event.screenXevent.screenY.
窗口坐标:这些坐标是从浏览器内容区域的左上角开始测量的.如果窗口垂直或水平滚动,则与文档的左上角不同.这很少是你想要的.在所有浏览器中,这些都在event.clientX和event.clientY中.
文档坐标:这些坐标是从HTML文档的左上角开始测量的.这些是您最常需要的坐标,因为这是定义文档的坐标系.

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

希望这对你有所帮助.

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

dja*_*fan 11

为什么在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();
Run Code Online (Sandbox Code Playgroud)