使用geckodriver将鼠标移动到位置

时间:2017-05-22 16:03:11

标签: selenium selenium-webdriver intern geckodriver

我正在使用Selenium实习测试来自动化功能测试(link)。

我们需要能够针对多种浏览器运行。所以我们更多地关注 Chrome ,但我们也能够针对 IE Firefox 运行测试,但在geckodriver之后发布后我们切换到它,大多数暗示鼠标事件的测试都失败了,例如当我调用moveMouseTo()时我得到以下异常:

Executing: [mousemove: 7 false])
WARN - Exception thrown
org.openqa.selenium.UnsupportedCommandException: mouseMoveTo

我发现了一个关于悬停在元素上的类似问题,我设法通过执行来自moveMouseTo函数的代码使其与实习生一起工作,就像我们已经破坏鼠标事件一样,所以我只是评论说如果相关到brokenMouseEvents

//if (this.capabilities.brokenMouseEvents) {
   if(element){
      return element.getPosition().then(function(position){
            return self.execute(simulateMouse, [ {
                        action: 'mousemove',
                        position: position,
                        element: element,
                        xOffset: xOffset,
                        yOffset: yOffset
                    } ]).then(function (newPosition) {
                        self._lastMousePosition = newPosition;
                    });
                })
    } else{
           return self.execute(simulateMouse, [ {
                action: 'mousemove',
                position: self._lastMousePosition,
                element: element,
                xOffset: xOffset,
                yOffset: yOffset
            } ]).then(function (newPosition) {
                self._lastMousePosition = newPosition;
            });
      }
//}

但这样做会打破滚动,因为moveMouseTo()也可用于滚动元素,我们也不能使用它,因为moveMouseTo()函数是在节点模块中定义的,并且它会失败我们尝试在Jenkins中运行测试。 另一个想法,我认为当我们针对 Firefox 运行时,将构建一个包装函数来处理这种情况,在这种情况下我们应该执行另一个代码,对于其他浏览器我们可以调用moveMouseTo(),但在这种情况下,我不确定如何解决滚动问题。

有没有人有任何想法?

谢谢!

2 个答案:

答案 0 :(得分:0)

我想转移到可以使用Actions class

的元素
Actions ac= new Actions(driver);
ac.movetoElement().build().preform();

答案 1 :(得分:0)

尝试将其移至元素:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(arguments[0].scrollIntoView();", webElement);