即使鼠标不移动,Selenium MoveToElement也会引发onmousemove事件?

时间:2017-05-10 13:00:28

标签: javascript vb.net selenium

我正在与Selenium进行一些测试,在此期间我发现了一些我不确定的奇怪行为......

似乎Move类的MoveToElement方法导致window.onmousemove事件反复触发,即使在鼠标移动完成后(这是我的问题以及我在这里发布的原因,而不是onmousemove行为时的行为)人类正在与浏览器进行交互。)

我希望onmousemove事件仅在鼠标实际移动时触发,就像它应该的那样。谢谢你的帮助。

这是我的代码。

Public Class Form1
Imports OpenQA.Selenium
Imports OpenQA.Selenium.IE
Imports OpenQA.Selenium.Interactions

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim driver As New InternetExplorerDriver()
        driver.Navigate.GoToUrl("http://127.0.0.1:1337/showmouseposition.html")
        driver.Keyboard.PressKey(Keys.F12)
        Dim builder As New Actions(driver)
        Dim ele As IWebElement = driver.FindElement(By.Id("myButton"))
        builder.MoveToElement(ele)
        builder.Perform()
    End Sub

End Class

showmousepositions.html

<html>
<head>
</head>
<body>
    <input type="text" id="x">
    <input type="text" id="y">
    <button type="button" id="myButton" onclick="alert('clicked btn')">Click Me!</button>

    <script>
        window.doLogging = true;
        document.onmousemove = function(e) {
            var dot, eventDoc, doc, body, pageX, pageY;

            event = event || window.event; // IE-ism

            // If pageX/Y aren't available and clientX/Y are,
            // calculate pageX/Y - logic taken from jQuery.
            // (This is to support old IE)
            if (event.pageX == null && event.clientX != null) {
                eventDoc = (event.target && event.target.ownerDocument) || document;
                doc = eventDoc.documentElement;
                body = eventDoc.body;

                event.pageX = event.clientX +
                  (doc && doc.scrollLeft || body && body.scrollLeft || 0) -
                  (doc && doc.clientLeft || body && body.clientLeft || 0);
                event.pageY = event.clientY +
                  (doc && doc.scrollTop  || body && body.scrollTop  || 0) -
                  (doc && doc.clientTop  || body && body.clientTop  || 0 );
            }

            // Use event.pageX / event.pageY here
            if (doLogging) {
                window.x.value = event.pageX;
                window.y.value = event.pageY;
                console.log(event.pageX + "," + event.pageY);
            }
        }
    </script>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

你尝试过build.perform()吗?

 Public Class Form1
 Imports OpenQA.Selenium
 Imports OpenQA.Selenium.IE
 Imports OpenQA.Selenium.Interactions

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles 
    Button1.Click
    Dim driver As New InternetExplorerDriver()
    driver.Navigate.GoToUrl("http://127.0.0.1:1337/showmouseposition.html")
    driver.Keyboard.PressKey(Keys.F12)
    Dim builder As New Actions(driver)
    Dim ele As IWebElement = driver.FindElement(By.Id("myButton"))

    builder.MoveToElement(ele).Build.Perform()

End Sub

End Class

答案 1 :(得分:0)

虽然这只是一个部分解决方案,但我使用FireFox驱动程序的正常onmousemove行为。因此,对于我的使用,我可以忍受不能使用具有可靠的onmousemove行为的IE(当我离开Selenium IE浏览器一夜之间导致导航到测试页面时,它实际上导致IE中的内存泄漏,该测试页面是console.logging onmousemove坐标而且泄漏是由于只有1次调用MoveToElement引起的,onmousemove是在无限循环中调用的,等等。)

但是,如果有人可以帮助我使用IE驱动程序,我会将你标记为答案,并会感谢你的帮助。我正在使用Seleniums站点的最新版本的IEServerDriver.exe,并在Windows 10上安装了IE 11。