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.
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
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
Mukesh Otwani says
Hi Bhargavi, JS Click should work even if it is overlapping. Please send me logs and code too mukeshotwani@learn-automation.com
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. 🙂
Mukesh Otwani says
Cheers, Tanushri..:)
sivaprakash says
Hi Mukesh,
Thanks for the third solution. its work for me.
thanks
Mukesh Otwani says
Thats great..:)
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
Mukesh Otwani says
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.
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?
Mukesh Otwani says
Hi Mani,
Ideally, it should not fail but if it is failing then based on exception we need to fix the script.
Mukesh Otwani says
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 http://learn-automation.com/explicit-wait-in-selenium-webdriver/
Anija says
Thank you so much…….
Mukesh Otwani says
Hi Anija,
I am glad to see your comments. Keep in touch with my blog…:)
Lakshmi says
Hi Mukesh,
Please tell me the difference between ElementNotVisibleException and ElementNotFoundException?
Mukesh Otwani says
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
Vanitha says
mukesh i have some doubts on selenium. please give me ur mail id
Mukesh Otwani says
support@learn-automation.com kindly share your doubts with us.
preetam singh says
Hi Mukesh
i have one question
suppose we have three element of same xpath
then how will we use appropriate element
Mukesh Otwani says
Hi Preetam you can use index if multiple element found. http://learn-automation.com/difference-between-findelement-and-findelements-in-selenium-webdriver/
Preetam Singh says
thnx Mukesh
how can i find all broken links from footer section
Mukesh Otwani says
Hi Preetam,
please visit this link http://learn-automation.com/find-broken-links-using-selenium/
mayank chincholkar says
Nice post, helped me a lot!
Mukesh Otwani says
Good to hear that mayank 🙂 keep in touch
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
Mukesh Otwani says
Hi Bala,
I can see multiple button for this name so you can check which button to click.
Balakrishna says
I have taken xpath by using Selenium IDE to click the First Request Consultation button …but its giving above exception
Mukesh Otwani says
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.
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!
Mukesh Otwani says
Wow Cheers Nikhil.
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
Mukesh Otwani says
Cheers Sumit.
Ricardo says
I was trying Explicit Wait my way and it wasn’t working. Explicit Wait your way worked for me, thanks!
Mukesh Otwani says
Great Ricardo, I am glad it helped you.
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();
Mukesh Otwani says
Hi Amit,
Try below methods today https://www.youtube.com/watch?v=2eEr0jtmpwE
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.
Mukesh Otwani says
Hi Hemanth,
I would suggest you to start automating below sample apps.
http://enterprise.demo.orangehrmlive.com/symfony/web/index.php/auth/login
sifat says
Awesome bro!
3rd option worked for me.
Mukesh Otwani says
Hey Sifat Cheers
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
Mukesh Otwani says
Cheers Anand.
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
Mukesh Otwani says
Cheers Priscilla 🙂 I am glad it worked for you.
ashwinishanbhogh says
Hi, third solution for elementnot visible exception workedout for me..thanks a lot
Mukesh Otwani says
Cheers Ashwin 🙂
Ashutosh Mishra says
Hello Mukush..:)
can we automate QC
Mukesh Otwani says
No Ashutosh. Webdriver mainly deals with Web Browsers.
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();
Mukesh Otwani says
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
http://learn-automation.com/how-to-write-dynamic-xpath-in-selenium/
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.
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.
srreddy says
Hi Mukesh,you doing well
srreddy says
when to use unregistered webdriver event listener
Mukesh Otwani says
For listener I found 1 nice article check below link
http://www.toolsqa.com/selenium-webdriver/event-listener/
srreddy says
Hi…Mukesh
1.All Browsers support for File Upload(text,Excel,button)?
2.Difference between data driven and keyword driven framework
srreddy says
If u dont mind Give me your gmail id, I will send some doubts
Mukesh Otwani says
Thanks Srreddy 🙂
Gaurav Khurana says
Good to see Mukesh you are sharing other blogs on your website!!! cheers
Mukesh Otwani says
Thanks Gaurav, I have sent a message to your blog. kindly check.
Gaurav Khurana says
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/
Mukesh Otwani says
Congrates Gaurav 🙂 Nice post..