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. 

16 comments:

  1. hi nitin,

    in which selenium version you have faced Element not visible exception ?
    And on which browser ?

    Currently i m facing same problem with IE8 & selenium 2.b3 version. But i guess selenium b3 version doesnot seems to be stable so can you please tell me which selenium version you tried ?

    ReplyDelete
  2. can you provide javascript syntax to run for hidden buttons?

    ReplyDelete
  3. Hi Harvest ,
    Thanks for the reply

    I have element with class name called "OPTIONS" and it has display: none as attribute .
    just you need to change the attribute to display:block

    If options is sub menu then you can give index of sub option like I have given below
    ---
    "document.getElementsByClassName('OPTIONS')[1].style.display='block'";
    ---
    else
    ---
    "document.getElementsByClassName('OPTIONS').style.display='block'";
    ---

    You can try this from Firebug.Try to change the attribute from Firebug and see that element changes on main HTML page
    ***************************************************


    JavascriptExecutor js=(JavascriptExecutor) driver;

    String script= document.getElementsByClassName('OPTIONS').style.display='block'";
    js.executeScript(script);
    *************************************************

    Apart from this there might be user defined javascript attached to element , sometimes developer add javascript to element which will perform some checks and then enable the element . You can directly invoke that script from above mentioned code.

    Let me know if anything else is needed.

    Thanks
    Nitin

    ReplyDelete
  4. What if there is not javascript behind button

    I am getting this error

    org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
    Command duration or timeout: 50.11 seconds
    Build info: version: '2.20.0', revision: '16008', time: '2012-02-27 19:03:04'
    System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_26'
    Driver info: driver.version: RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:170)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:123)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:438)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:231)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:72)

    ReplyDelete
    Replies
    1. DP , I not only meant that Javascript function will be attached to the element . Element must have some attributes and you can change that attributes using javascript like I have explained in my comment

      http://nitinpurswani.blogspot.com/2010/09/elementnotvisible-exception-in-selenium.html?showComment=1328159891667#c7697429672039286050.

      check if this snippet helps
      =========================

      element=document.getElementById("male");
      element.removeAttribute("disabled",1);

      input type="radio" name="male" id="male" disabled

      ===============================
      If above javacript code ran then raido buttons attribute will get change , like that with help of javascript you can change the attribute of any element


      Thanks
      Nitin

      Delete
  5. Hi my ex;

    OpenQA.Selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
    Command duration or timeout: 16 milliseconds
    Build info: version: '2.18.0', revision: '15704', time: '2012-07-21 01:03:50'
    System info: os.name: 'Windows 2003', os.arch: 'x86', os.version: '5.2', java.version: '1.6.0_35'
    Driver info: driver.version: RemoteWebDriver
    at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
    at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
    at OpenQA.Selenium.Remote.RemoteWebDriver.InternalExecute(String driverCommandToExecute, Dictionary`2 parameters)
    at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary`2 parameters)
    at OpenQA.Selenium.Remote.RemoteWebElement.Click()



    my code ;


    driver.FindElement(By.Id("ctl00_ctlHeader1_lnkResidenceSale")).Click();

    driver.FindElement(By.Id("ctl00_cphContent_ctlRealtySearchBox1_ctlRealtyFullTextSearchBox1_txtSearch")).Click();

    driver.FindElement(By.Id("ctl00_cphContent_ctlRealtySearchBox1_ctlRealtyFullTextSearchBox1_txtSearch")).Clear();

    driver.FindElement(By.Id("ctl00_cphContent_ctlRealtySearchBox1_ctlRealtyFullTextSearchBox1_txtSearch")).SendKeys("istanbul");

    driver.FindElement(By.XPath("//a[@id='ctl00_cphContent_ctlRealtySearchBox1_ctlRealtyFullTextSearchBox1_btnTextSearch']/img")).Click();

    driver.FindElement(By.Id("ctl00_cphContent_ctlRealtyList1_rptRealtyList_ctl33_lnkOverlay")).Click();

    Cleanup();

    ReplyDelete
  6. I was facing similar problems , all I did was just paut a wait for 5 seconds and it worked fine.

    ReplyDelete
  7. Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
    Command duration or timeout: 16 milliseconds

    ReplyDelete
  8. Can u please give solution?

    i m a beginer

    ReplyDelete
  9. I was facing similar problems , all I did was just paut a wait for 10 seconds and it worked fine.

    ReplyDelete
  10. I was facing similar problems , all I did was just paut a wait for 10 seconds and it worked fine.

    ReplyDelete
  11. i am facing this problem right now and the web page was build using polymer, i dont know if is the problem but iḿ using the code driver.findElement(By.cssSelector.. and give me a exception element not visible

    ReplyDelete
  12. i am facing this problem right now and the web page was build using polymer, i dont know if is the problem but iḿ using the code driver.findElement(By.cssSelector.. and give me a exception element not visible

    ReplyDelete
  13. I am facing problem right now that i want to click on save button but it shows element is not visible.I have tried with xpath and css but it cannot click that element.I want to use execute javascript but i dont know to use it.I am sending class="btn btn-primary btn-sm custom-button" type="submit" value="Save" style="margin-right: 15px;"/> can you how to write execute javascript for this.I have stuck to this point since long time because know java scripting.

    ReplyDelete
    Replies
    1. I dont know java scripting

      Delete
  14. It worked for me , xpath was confusing , rather used driver.findElement(By.name("User_Name"));

    ======================================

    WebElement elem = driver.findElement(By.name("User_Name"));
    String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";

    ((JavascriptExecutor) driver).executeScript(js, elem);

    ReplyDelete