问题
I am new to Selenium. I am using gridlastic as test environment.
I have gone through Actions class for selenium which has methods to move mouse as well as got the mouse instance via web driver by calling getMouse() and tried to move the mouse but dint succeed.
@Test(enabled = true)
public void test_site() throws Exception {
Coordinates elementLocation = null;
driver.get("https://www.amazon.com");
Mouse mouse = ((HasInputDevices) driver).getMouse();
System.out.println(mouse.toString());
if(mouse==null) {
System.out.println("mouse is null");
}
WebElement element1=driver.findElementByXPath("//*[@id=\"a-autoid-0-announce\"]");
elementLocation = ((Locatable) element1).getCoordinates();
mouse.mouseMove(elementLocation);
Thread.sleep(5000); //slow down for demo purposes
}
Also tried using the actions class as well
@Test(enabled = true)
public void test_site() throws Exception {
driver.get("https://www.google.com/ncr");
Actions builder = new Actions(driver);
builder.
moveByOffset( 100, 1 )
.build().perform();
Thread.sleep(10000); //slow down for demo purposes
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("webdriver");
element.submit();
Thread.sleep(5000); //slow down for demo purposes
}
Still dint succeed , the mouse pointer remains at position (0,0) always. Can anyone help how to do it.
Please don't suggest using Robot Class from JAVA as the test environment is gridlastic and it doesn't work for it.
I also tried using the javascript executor, but its not possible as mouse cursor is controlled by operating system. I thought of changing values of window object clientX and clientY , but those are readonly as per documentation.
回答1:
This seems very strange, but if you try the following code, you will see, that you do not see the moving mousepointer:
PointerInput p = new PointerInput(PointerInput.Kind.MOUSE, "MyMouse");
Interaction i = p.createPointerMove(Duration.ofSeconds(2), PointerInput.Origin.fromElement(element1), 5, 5);
Actions builder = new Actions(driver);
Action mouseOverHome = builder
.tick(i).click()
.build();
mouseOverHome.perform();
I added the click to demonstrate the effect. I had to work with the shopping cart at //*[@id=\"nav-cart\"]
. Your xpath expression was not visible for me. You will notice the mouseover effect of the cart icon before the click.
来源:https://stackoverflow.com/questions/54595056/how-to-move-visible-mouse-pointer-using-selenium-web-driver