与IE的Selenium Hover元素

我有一个HTML div标签,在div内部有一个元素,当鼠标进入其边界时出现。 现在我想点击鼠标进入或hover时可见的元素。

问题:元素开始闪烁。 浏览器:IE8

我正在使用下面的代码

  IWebElement we = addToBasket.FindElement(By.Id("MyBox")); action.MoveToElement(we).MoveToElement(driver.FindElement(By.Id("plus-icon"))).Click().Build().Perform(); 

有什么建议为什么眨眼?

由于IE驱动程序称为“持久性hover”的function,该元素闪烁。 此function具有可疑价值,但由于脑死机方式IE(浏览器,而不是驱动程序)在使用SendMessage API时响应WM_MOUSEMOVE消息 ,因此是必需的。

你有几个选择。 您可以使用以下代码关闭持久性hover:

 InternetExplorerOptions options = new InternetExplorerOptions(); options.EnablePersistentHover = false; IWebDriver driver = new InternetExplorerDriver(options); 

请注意,尽管这会使您在尝试hover时遇到物理鼠标光标在屏幕上的奇思妙想。 如果这是不可接受的,您可以采取其他几种方法 。 首先,您可以关闭所谓的“本机事件”,这会导致驱动程序仅依赖于合成的JavaScript事件。 由于仅依靠JavaScript来合成鼠标事件,因此这种方法有其自身的缺陷。

 InternetExplorerOptions options = new InternetExplorerOptions(); options.EnableNativeEvents = false; IWebDriver driver = new InternetExplorerDriver(options); 

最后,您可以从使用默认的SendMessage Windows API迁移到使用更正确的SendInput API的代码。 这是通过RequireWindowFocus属性完成的。 它的缺点是鼠标输入在系统中以非常低的水平注入,这要求IE窗口成为系统上的前景窗口。

 InternetExplorerOptions options = new InternetExplorerOptions(); options.RequireWindowFocus = true; IWebDriver driver = new InternetExplorerDriver(options); 

最后要注意的是,不要试图一次性设置所有这些属性; 选择一种方法并坚持下去。 其中一些是互斥的,它们之间的相互作用是不确定的。

这对我有用。

 WebElement element = driver.findElement(By.xpath("element xpath")); Locatable hoverItem = (Locatable) element; Mouse mouse = ((HasInputDevice) driver).getMouse(); mouse.mouseMove(hoverItem.getCoordinates());