如何使用Selenium Web驱动程序移动可见的鼠标指针? [英] How to move visible mouse pointer using selenium web driver?

查看:471
本文介绍了如何使用Selenium Web驱动程序移动可见的鼠标指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Selenium的新手. 我正在使用 gridlastic 作为测试环境.

I am new to Selenium. I am using gridlastic as test environment.

我经历了硒类的Actions类,该类具有移动鼠标的方法,并且通过调用getMouse()通过Web驱动程序获取了鼠标实例,并尝试移动鼠标但没有成功.

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


    }

也尝试过使用动作类

@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
    }

仍然成功,鼠标指针始终保持在位置(0,0).任何人都可以帮忙.

Still dint succeed , the mouse pointer remains at position (0,0) always. Can anyone help how to do it.

请不要建议使用JAVA中的Robot Class,因为测试环境是网格弹性的,并且不适用于该环境.

Please don't suggest using Robot Class from JAVA as the test environment is gridlastic and it doesn't work for it.

我也尝试使用javascript执行程序,但由于鼠标光标受操作系统控制,因此无法执行.我想到了更改窗口对象clientX和clientY的值,但是根据文档,它们是只读的.

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.

推荐答案

这似乎很奇怪,但是如果您尝试下面的代码,将会看到,看不到正在移动的鼠标指针:

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

我添加了单击以演示效果.我不得不使用//*[@id=\"nav-cart\"]上的购物车.您的xpath表达式对我不可见.单击之前,您会注意到购物车图标的鼠标悬停效果.

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.

这篇关于如何使用Selenium Web驱动程序移动可见的鼠标指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆