Monday, March 19, 2012

Webdriver - Mouse over events (Hover).

Webdriver - Mouse over events (Hover).

Read the following text from the webdriver team with respect to hovering problem.

When you attempt to hover over elements, and your physical mouse cursor is within the boundaries of the IE browser window, the hover will not work. More specifically, the hover will appear to work for a fraction of a second, and then the element will revert back to its previous state. The prevailing theory why this occurs is that IE is doing hit-testing of some sort during its event loop, which causes it to respond to the physical mouse position when the physical cursor is within the window bounds. The WebDriver development team has been unable to discover a workaround for this behavior of IE.

There are few objects and images on my web page where click() functionality is not working. I have taken lot of approaches, but the best solution found so far is below

The first statement will focus the element, next statement actually click the object.

I have another scenario where Image is displayed when I bring the mouse pointer on that row. To handle this situation I have used Actions class.

Below image shows display of icon when user bring the mouse pointer.

Webdriver code to click above pointed image.

Actions action = new Actions(driver);

action.moveToElement(driver.findElement("ctl00_CphePurchase_agvApprovalRequisitions$3$0$aReqNumber")));//id of requisition number
action.perform();"ctl00_CphePurchase_agvApprovalRequisitions$3$0$imgBtnTrackStatus"))); //id of the image

Above code will not move the actual mouse pointer, but it will convey to IE that it has received a mouse input in that particular location.

For better understanding read the text from Jim (IE driver project lead).

Let me try to explain this. When simulating mouse movements with 
WebDriver, the actual mouse cursor does not move. Let me repeat that 
for emphasis. The actual physical mouse pointer on the screen *will* 
*not* *move*. If you are expecting that, you'll be disappointed. 

What WebDriver does on Windows is it sends the same messages to the IE 
window that it would receive from the input manager by the actual 
mouse. You won't see a change in the pointer on the screen, but if the 
element on the page responds to mouseover events, it should react as 
if you moved the mouse over it. 

The advantage here is that if something like an alert() method is 
called in the element's mouseOver event, WebDriver can still handle 
it. Additionally, it's a more accurate representation of the actual 
mouse movement, firing ancillary events like mouseEnter and so on. 
Contrast this approach with that of Selenium RC, which simply fires 
the mouseOver event directly via JavaScript. 


Still if you want to move the mouse pointer physically, you need to take different approach using Robot class

Point coordinates = driver.findElement("ctl00_portalmaster_txtUserName")).getLocation();
Robot robot = new Robot();

Webdriver provide document coordinates, where as Robot class is based on Screen coordinates, so I have added +120 to compensate the browser header.

Screen Coordinates: These are coordinates measured from the top left corner of the user's computer screen. You'd rarely get coordinates (0,0) because that is usually outside the browser window. About the only time you'd want these coordinates is if you want to position a newly created browser window at the point where the user clicked.
In all browsers these are in event.screenX and event.screenY.

Window Coordinates: These are coordinates measured from the top left corner of the browser's content area. If the window is scrolled, vertically or horizontally, this will be different from the top left corner of the document. This is rarely what you want.
In all browsers these are in event.clientX and event.clientY.

Document Coordinates: These are coordinates measured from the top left corner of the HTML Document. These are the coordinates that you most frequently want, since that is the coordinate system in which the document is defined.

For more details select this link.



  1. i have a problem using action builder after updating to 2.28 with Firefox 13.0

    this is the code i used
    WebElement change_language= driver.findElement(By.xpath("//*[@id='change_language']/p"));
    WebElement french= driver.findElement(By.xpath("//*[@id='change_language']/div/ul/li[2]/a"));
    Actions builder = new Actions(driver);

    any suggestions welcome

  2. i have a problem while clicking sublink

    WebDriver driver=new FirefoxDriver();

    after login to the page we need to clik AddEmployee under PIM:

    please reply with code

  3. hi
    by using robot class i am able to hover and click on the subLink but after that webdriver is not able to find locators in the new page ... do i need to release mouseMove or anything likewise can you please help me in this issue.

    1. Find the different window handles available and switch to it.

  4. Hi Bharath,

    I tried using the code approach of

    Point coordinates = driver.findElement("ctl00_portalmaster_txtUserName")).getLocation();
    Robot robot = new Robot();

    and this did not work.

    The scenario is as follows.

    Go to Do Mouseover Find your nearest (Main Menu A, I am able to do this using actions driver concept) then Mouse over Submenu B which is ATM (this also I am able to do this) and then click on SubMenu C "All ATMs" (this is the one I am NOT able to to)

    Could you kindly give inputs to this scenario.



  5. Hi bharat,

    Thank you sooo much.. Robot class is working fine for my application.