Aman Aman - 6 months ago 55
Java Question

Selenium Hover elements with IE

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.

Issue: the element starts blinking.
Browser: 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?

Answer

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 messages when using the SendMessage API.

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 approaches you 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.

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.

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.

Comments