Java 如何使用robot类在Selenium中基于webelement而不是getlocation的xy坐标执行mousemove?

Java 如何使用robot类在Selenium中基于webelement而不是getlocation的xy坐标执行mousemove?,java,selenium,selenium-webdriver,mouseevent,mousemove,Java,Selenium,Selenium Webdriver,Mouseevent,Mousemove,问题陈述:如何使用robot类基于webelement而不是Selenium中getlocation的xy坐标执行mousemove 下面的代码片段通常用于mousemove,它根据x-y坐标移动鼠标 Robot robot = new Robot(); robot.mouseMove(to_x, to_y); 但是,有没有一种方法可以基于webelement而不使用getlocation来移动robot类的鼠标 Example: webelement drag = driver

问题陈述:如何使用robot类基于webelement而不是Selenium中getlocation的xy坐标执行mousemove

下面的代码片段通常用于mousemove,它根据x-y坐标移动鼠标

 Robot robot = new Robot();
 robot.mouseMove(to_x, to_y); 
但是,有没有一种方法可以基于webelement而不使用getlocation来移动robot类的鼠标

Example:
    webelement drag = driver.findelement(by_xpath('xpaht'))
    Robot robot = new Robot();
    robot.mouseMove(drag); //Is there any way to do it like this in selenium java
我不想使用actions类的movetoelement

Selenium 3.141/Java
Chromedriver 76和FF浏览器

您可以使用Locatable类-

Locatable hoverItem = (Locatable) driver.findElement(By.xpath("element xpath"));enter code here
int y = hoverItem.getCoordinates().getLocationOnScreen().getY();
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+y+");");

希望对您有所帮助。

谢谢。我尝试了这个代码段,但我发现异常为“java.lang.ClassCastException:org.openqa.selenium.remote.RemoteWebElement无法强制转换为org.openqa.selenium.interactions.internal.Locatable”您可以试试这个-element=driver.findElement(By.xpath(“element xpath”))公共坐标getCoordinations(){(element instanceof Locatable){return((Locatable)元素).getCoordinates();}上述解决方案也产生了相同的异常。getCoordinates将返回可能依赖于y坐标的x-y坐标。是否有方法避免这种依赖关系,并直接使用robot.mousemove(toelement)。