Java Selenium Hover 元素与 IE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19662045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium Hover elements with IE
提问by Aman
I have an HTML div
tag and inside the div there is an element which appears in when mouse enters its boundaries. Now I want to click on the element which becomes visible on mouse entering or hovering.
我有一个 HTMLdiv
标签,在 div 内有一个元素,当鼠标进入其边界时会出现在其中。现在我想点击鼠标进入或悬停时可见的元素。
Issue: the element starts blinking. Browser: IE8
问题:元素开始闪烁。浏览器:IE8
I am using the code below
我正在使用下面的代码
IWebElement we = addToBasket.FindElement(By.Id("MyBox"));
action.MoveToElement(we).MoveToElement(driver.FindElement(By.Id("plus-icon"))).Click().Build().Perform();
Any suggestion why its blinking?
任何建议为什么它闪烁?
采纳答案by JimEvans
The element is blinking because of a feature of the IE driver called "persistent hovers." This feature is of dubious value, but is required because of the brain-dead way IE (the browser, not the driver) responds to WM_MOUSEMOVE
messageswhen using the SendMessage
API.
由于 IE 驱动程序的一项称为“持久悬停”的功能,该元素正在闪烁。此功能的价值值得怀疑,但由于 IE(浏览器,而不是驱动程序)在使用API时响应WM_MOUSEMOVE
消息的脑残方式,因此需要此功能SendMessage
。
You have a few options. You can turn persistent hovers off by using code like the following:
你有几个选择。您可以使用如下代码关闭持久悬停:
InternetExplorerOptions options = new InternetExplorerOptions();
options.EnablePersistentHover = false;
IWebDriver driver = new InternetExplorerDriver(options);
Be aware, though that this will subject you to the whims of where the physical mouse cursor is on the screen when you attempt to hover. If that's not acceptable, you have a couple of other approachesyou could take. First, you could turn off so-called "native events," which would cause the driver to rely solely on synthesized JavaScript events. This approach has its own pitfalls, due to relying only on JavaScript to synthesize the mouse events.
请注意,尽管当您尝试悬停时,这会使您受到物理鼠标光标在屏幕上的位置的影响。如果这是不可接受的,您可以采用其他几种方法。首先,您可以关闭所谓的“本机事件”,这会导致驱动程序仅依赖于合成的 JavaScript 事件。由于仅依赖 JavaScript 来合成鼠标事件,因此这种方法有其自身的缺陷。
InternetExplorerOptions options = new InternetExplorerOptions();
options.EnableNativeEvents = false;
IWebDriver driver = new InternetExplorerDriver(options);
Finally, you could migrate from using the default SendMessage
Windows API to code that uses the more correct SendInput
API. This is done with the RequireWindowFocus
property. Its drawback is that the mouse input is injected at a very low level in the system, which requires the IE window to be the foreground window on the system.
最后,您可以从使用默认SendMessage
Windows API迁移到使用更正确SendInput
API 的代码。这是通过RequireWindowFocus
属性完成的。它的缺点是鼠标输入在系统中的注入非常低,这就要求IE窗口成为系统的前台窗口。
InternetExplorerOptions options = new InternetExplorerOptions();
options.RequireWindowFocus = true;
IWebDriver driver = new InternetExplorerDriver(options);
As a final note, do not attempt to set all of these properties at once; pick an approach and stick with it. Several of them are mutually exclusive, and the interaction between them is undefined.
最后要注意的是,不要试图一次设置所有这些属性;选择一种方法并坚持下去。它们中的几个是相互排斥的,它们之间的相互作用是不确定的。
回答by AyeVeeKay
This worked for me.
这对我有用。
WebElement element = driver.findElement(By.xpath("element xpath"));
Locatable hoverItem = (Locatable) element;
Mouse mouse = ((HasInputDevice) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());