Saturday, September 25, 2010

ElementNotVisible exception in Selenium WebDriver

Problem I was facing 
Recently I was using Selenium Webdriver for one of my project and got ElementNotVisible exception, even though element was visible on screen.I went through the Webdriver old forum posts but could not find exact solution. But forum posts and webdriver FAQ gave me some good pointers to solve the problem.

Cause of the problem I figured out

This issue occur to me due to  2 reasons.

First reason:-I was using relative Xpath and same Xpath exists on page more than once.There could be many reason for that, but in my case element I was trying to click, was under a dialog box and HTML page has template for that dialog box. Template Dialog box was hidden on page.When I was using relative Xpath,selenium was trying to hit hidden element inside the template dialog box and giving exception ElementNotVisibleException. 

solution:- Change Xpath in your code, so  Selenium don't get confuse between two elements.

Second reason:-I was trying to click a button which changes on hovering mouse over the button. There where two elements related to button first one was shown without hovering mouse and other gets shown when you hover mouse over button. Element shown on hovering mouse was performing actual function using a javascript and that element was not visible until you hover mouse over button .Clicking on that directly was giving ElementNotVisibleException.

solution:- For hidden links and button you can directly run the javascript behind the hidden button which will serve the purpose.