Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 920
Aman Avatar asked Oct 29 '13 15:10

Aman


1 Answers

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.

like image 112
JimEvans Avatar answered Sep 23 '22 02:09

JimEvans