Basic Selenium

How to Solve ElementNotVisibleException in Selenium Webdriver

Hello, Guys, Welcome back to Selenium tutorial, today we will see how to handle element not visible  exception in Webdriver.

I faced this exception number of times, I struggled a lot while searching solution, and finally, I got so many solutions to solve this exception.

Before jumping to ElementNotVisibleException you should check below two posts that actually will help you to understand exception better.

How to handle Exception in Selenium

What are different exception is available in Selenium.

 

Full Exception document is available here- https://selenium.googlecode.com/git/docs/api/py/common/selenium.common.exceptions.html

 

Let us see what the reason behind this exception is:

Reasons for ElementNotVisibleException in Selenium Webdriver

Reason 1- Duplicated XPATH

While writing xpath for your application, you might have taken xpath that is matching with more than 1 element, in this case, Selenium will throw Element, not the visible exception.

If you are new to Selenium and facing issues while writing XPath then please check below post which will help you to write xpath from basic to advance level.

How to write Dynamic Xpath in Selenium Webdriver

Reason 2-

If you are trying to access some particular element on Webpage that is not currently visible, in this case also you will get the Element, not visible exception.

 

 

ElementNotVisible Exception in Selenium

ElementNotVisible Exception in Selenium

Solutions for ElementNotVisibleException in Selenium Webdriver

First Solution: Try to write unique XPATH  that matches with a single element only.

 

Second Solution:

Use Explicit wait feature of Selenium and wait till the element is not visible. Once it is visible then you can perform your operations.

Syntax for Explicit wait

new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='index.html']")));

 

Explanation- In above example, Selenium will wait 30 seconds until the element is not visible and once it is visible, Selenium will perform an action and will move on to the next step.

You can also give a try using JavaScript click that forcefully click on Element 

 

Third solution:

This actually works for me a number of times. I was struggling with the OK button in my application. Even I was writing unique xpath but still I was facing Element Not Visible exception.  I have tried below code that solved my issue.

int ok_size=driver.findElements(By.xpath("//button[text()='OK']")).size();

driver.findElements(By.xpath("//button[text()='OK']")).get(ok_size-1).click();

Explanation

First, I have taken the size of the element then in next statement, I took the first element from the list and I clicked on OK Button.

I have seen so many scenarios where I solved my issue using above scenario.

I also faced lots of Hidden Web Elements in my web application and used below trick which helped me a lot.Check out below youtube video for the same.

 

Hope this post is worth for you and your friends as well, so if you find this as useful then do share this post with your friends using sharing buttons.

Comment below if you are facing any issue.

Thanks for visiting my blog. Keep visiting. Have a nice day.

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

Related Posts

61 thoughts on “How to Solve ElementNotVisibleException in Selenium Webdriver

  1. Bhargavi says:

    Hi, Mukesh

    Thanks for the solution, I have tried all three, Still facing the issue.

    I have been trying to click an element in DOM, The Xpath is showing only 1 Result while Inspecting
    Still facing Element Not found Exception.
    Tried with Js Executor, WebDriverWait. I think the element is overlapped. That’s why its unable to locate the element.

    Please help me to find the ways to locate the element while automating to click it. Thanks

    1. Hi Bhargavi, JS Click should work even if it is overlapping. Please send me logs and code too mukeshotwani@learn-automation.com

  2. Tanushri says:

    Hi Mukesh,

    Thanks alot as the third solution solved my problem. I was continuously getting elementNotInteractableException but using ur solve it works fine. 🙂

    1. Cheers, Tanushri..:)

  3. sivaprakash says:

    Hi Mukesh,
    Thanks for the third solution. its work for me.
    thanks

  4. Tanu says:

    Hi Mukesh,

    I am facing an issue while executing a scenario.
    1- There is calendar (Bootstrap is used). As soon as we click on calendar button its opens a calendar and also next and prev arrow to select any month’s date.

    Issue is this. Not able to create the Xpath for next and prev button as element is not visible.

    check this and help: https://demos.telerik.com/kendo-ui/datetimepicker/index

    1. Hi Tanu,

      First enable calendar control so that calendar should get enabled then use this xpath //a[@aria-label=’Next’] for next month. Similarly use //a[@aria-label=’Previous’] for previous month control.

  5. Manikumar says:

    Hi When we ran scripts to Intellij scripts were running properly but wehn we trigger with Jenkins we are getting no such element found exception can you please guide us?

    1. Hi Mani,

      Ideally, it should not fail but if it is failing then based on exception we need to fix the script.

    2. Hi Manikumar,

      Kindly check whether application websitehas same navigation just like you are having through IntelliJ. Also use proper wait if you are getting issues with element https://vistasadprojects.com/mukeshotwani-blogs-v2/explicit-wait-in-selenium-webdriver/

  6. Anija says:

    Thank you so much…….

    1. Hi Anija,

      I am glad to see your comments. Keep in touch with my blog…:)

  7. Lakshmi says:

    Hi Mukesh,
    Please tell me the difference between ElementNotVisibleException and ElementNotFoundException?

    1. Hi Lakshmi,

      As per my knowledge, ElementNotFound is a superclass and its sub classes are NoSuchElementException, NoSuchFrameException, NoSuchWindowExcption etc(there could be more). So ElementNotFoundException can be categorized into other exceptions
      While ElementNotVisibleException happens an element is present on the DOM, but it is not visible, and so is not able to be interacted with. For more details: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/ElementNotVisibleException.html

  8. Vanitha says:

    mukesh i have some doubts on selenium. please give me ur mail id

  9. preetam singh says:

    Hi Mukesh
    i have one question
    suppose we have three element of same xpath
    then how will we use appropriate element

      1. Preetam Singh says:

        thnx Mukesh
        how can i find all broken links from footer section

  10. mayank chincholkar says:

    Nice post, helped me a lot!

    1. Good to hear that mayank 🙂 keep in touch

  11. Balakrishna says:

    Hi Mukesh,
    http://www.nextrow.com/adobe-experience-manager/
    In the page ,there is button named Request for consultation ,I try to locate the element by using xpath but i am getting exception org.openqa.selenium.ElementNotVisibleException: element not visible

    1. Hi Bala,

      I can see multiple button for this name so you can check which button to click.

      1. Balakrishna says:

        I have taken xpath by using Selenium IDE to click the First Request Consultation button …but its giving above exception

        1. Hi Balakrishna,

          Try with this xpath for first one –> //h2[text()=’Services’]/preceding::*[text()=’Request Consultation’]
          and this one for second one –> //h2[text()=’Services’]/following::*[text()=’Request Consultation’]
          I hope these two should work.

  12. Nikhil says:

    Hi Mukesh,

    In my case I had to apply filters to 3 different columns on a single page of my application. And each first element after filtering had same locator. 🙁

    I was literally trying this for 2 days and finally it worked using 3rd solution .

    Thank you so much worked like a gem even though at first glance it looked like pretty off-beat solution. Cheers!

  13. sumit says:

    Hi Thanks for the Third Solution. its work for me. i got stuck in this situation for long time. but now its resolve.
    Thanks

  14. Ricardo says:

    I was trying Explicit Wait my way and it wasn’t working. Explicit Wait your way worked for me, thanks!

    1. Great Ricardo, I am glad it helped you.

  15. Amit Chaudhary says:

    Hi Mukesh,

    Thanks for the valuable post.

    Third solution work for me number of times but today it is not working for me.

    What is the problem.

    Below is the code-
    WebElement we=driver.findElement(By.xpath(“.//*[@id=’TrackersMenue’]/a”));
    we.click();
    driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
    int menu=driver.findElements(By.xpath(“.//*[@id=’exerciseMenuId’]/a/span”)).size();
    driver.findElements(By.xpath(“.//*[@id=’exerciseMenuId’]/a/span”)).get(menu-1).click();

  16. Hemanth Govindu says:

    Hi Mukesh,
    I’ve some doubt regarding ElementNotVisibleException, While i’m automating bookmyshow.com, when ever I navigate to home page, it asks for the location to enter at the topmost right corner of the page. I used the unique xpath, still faced ElementNotVisibleException and I didn’t change anything and executed few hours after, it worked fine. Also used implicit wait. could you please clarify my confusion and reason behind the failure?

    Thanks in advance.

    1. Hi Hemanth,

      I would suggest you to start automating below sample apps.

      http://enterprise.demo.orangehrmlive.com/symfony/web/index.php/auth/login

  17. sifat says:

    Awesome bro!
    3rd option worked for me.

  18. Anand says:

    Third Solutions worked for me. Thank you so much. Was challenging to find your solutions on Google.

    Thanks a ton appreciated

    Thanks
    Anand

  19. Priscilla says:

    Hi ,
    Solution No:3 worked perfectly for me. I have tried other solution and banging my head for past 3 days. Got few expert help too but nothing worked out.
    Thanks for the Post

    1. Cheers Priscilla 🙂 I am glad it worked for you.

  20. Hi, third solution for elementnot visible exception workedout for me..thanks a lot

    1. Cheers Ashwin 🙂

  21. Ashutosh Mishra says:

    Hello Mukush..:)
    can we automate QC

    1. No Ashutosh. Webdriver mainly deals with Web Browsers.

  22. sergio pavez says:

    Hey, while your solution works (second solution) I still have the same issue when trying to access another dropdown inside the one that I’m waiting. I tried to do the same thing again (new WebDriverWait) but it does not work.

    Here are the lines that I’m using:

    driver.findElement(By.xpath(“.//*[@id=’t3-mainnav’]/div/div/div/div/ul/li[3]”)).click();

    new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“.//*[@id=’t3-mainnav’]/div/div/div/div/ul/li[3]/div/div/div/div/div/ul/li[6]”))).click();

    new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“.//*[@id=’t3-mainnav’]/div/div/div/div/ul/li[3]/div/div/div/div/div/ul/li[6]/div/div/div/div/div/ul/li[2]”))).click();

    1. Hi Sergio,

      Look like there is some issue with Xpath which you have written try to write xpath with custom methods which may fix this issue.

      Please check below article and try to make the changes and run again

      https://vistasadprojects.com/mukeshotwani-blogs-v2/how-to-write-dynamic-xpath-in-selenium/

  23. Prasad says:

    The workaround sounds good, will give a try, thank you for sharing.

    got a doubt the reason 1st reason (Reason 1- Duplicated xpath), if there are duplicate xpath, I believe, selenium will take action on the 1st xpath which will display in html, let me know if my understanding is wrong.

    1. Mukesh Otwani says:

      Hello Prasad,

      Nice observation, In my case when I write xpath and if it matches with more than 1 element, selenium always confuse which element to click and it throws exception.

      As per your comment it will click on first element but I am not sure. You can check from your end.

      1. srreddy says:

        Hi Mukesh,you doing well

        1. srreddy says:

          when to use unregistered webdriver event listener

          1. Mukesh Otwani says:

            For listener I found 1 nice article check below link

            http://www.toolsqa.com/selenium-webdriver/event-listener/

          2. srreddy says:

            Hi…Mukesh
            1.All Browsers support for File Upload(text,Excel,button)?
            2.Difference between data driven and keyword driven framework

          3. srreddy says:

            If u dont mind Give me your gmail id, I will send some doubts

        2. Mukesh Otwani says:

          Thanks Srreddy 🙂

          1. Good to see Mukesh you are sharing other blogs on your website!!! cheers

          2. Thanks Gaurav, I have sent a message to your blog. kindly check.

          3. Thanks Mukesh for taking time and sharing your views on the blog.

            Because of your motivation i am able to post a new post after a long time. Hope you like it
            http://udzial.com/extract-text-image/

          4. Congrates Gaurav 🙂 Nice post..

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.