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.
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.
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.
Solutions for ElementNotVisibleException in Selenium Webdriver
First Solution: Try to write unique XPATH that matches with a single element only.
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.
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.
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