In this post, we will learn how to handle “ElementNotVisibleException ” and “Element is not clickable exceptions”.
While running Selenium scripts, sometimes we face below exceptions:
“org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout”
Note: Coordinates are just for reference.
What is ElementNotVisibleException?
When webdriver is able to locate web element on current webpage but it is not visible on screen.
What is Element is not clickable exception:
When webdriver is able to locate web element and also visible in screen but overlapping by another web element.
I hope above one line explanation helped you in understanding difference between both the exceptions.
Why selenium webdriver throws above exception and how to resolve?
You are very good in writing locators and when you run scripts, you get above exception. You are confused why is it so?
Reason No 01: Not locating correctly
- When locator is incorrect, selenium webdriver throws “NoSuchElementException”. Above exception says, webdriver is able to locate element, but not able to click on desired element or element is not visible.
- But your locator might be wrong as well in case of nested web element. A web element inside web element is called as nested web element. For example: A button inside a div and that Div is inside a span. If you want to click on button, you must need to locate that specific button tag. If you locate span or div, it might throw above exceptions.
- Another example: An input box is inside a span. If you locate span and want to type something, it will throw some exceptions because span is not an input type.
Locate correct and specific element.
Reason No 02: Desired element is overlapped by another web element
- Now a days many application uses toaster messages. A toaster notification is way to provide short and meaningfull message. Generally a toaster message stays on screen for 3 seconds. See example below:
- Consider below scenario:
- You have a social media application similar to Facebook.
- You need to sign up. When you signup you are navigated to home page and a toaster message appears stating signup is successful.
- You need to click on logout. Logout button is present in right top corner where toaster message also appears.
- When we do above flow, when we want to click on Logout button, you might get above exception as Logout button is overlapped by toaster message. Webdriver will not be able to click on Logout button and throws a perfect exception stating other element would receive the click.
- You need to wait till toaster message disappears. And we know how to wait in selenium.
- To solve above issue, You must use wait provided by selenium. Now you will ask which wait we should use?
- Thread.sleep(value): It is not advisable. You never know how much time you need to make webdriver sleep. It might increase your test execution time drastically and would not serve purpose always.
- Implicit wait: This will not work always. Implicit wait will make webdriver wait till it does not find it. Here issue is that webdriver finds web element but could not click on it or element i snot visible. Implicit wait can not stop webdriver once web element is found. This wait does not bother if element is displayed or not.
- Explicit wait: This is called dynamic wait and we should use it. ExpectedConditions class provides so many condition till you want to wait. We have condition which we can use to wait till element is displayed. It will make webdriver wait till element is displayed on UI not till the time element is found.
WebDriverWait wait= new WebDriverWait(driver,15);
4. Fluent wait: This is the best wait I feel. It provides you flexibility to create your own condition to wait for.
Note: If you want to learn more about waits in selenium , refer below links:
Reason No 03: Ajax calls
- Selenium can recognize and wait when whole page is loading but can not recognize loading through AJAX calls.
- When AJAX calls are in progress and you want to perform some action on a web element which is still loading because of AJAX calls. In this case, you can get NoSuchElementException or Element is not clickable(If action is click) or Element is not visible exceptions.
- Solution is same as I explained in Reason No 02.
Reason No 04: Webdriver is unable to auto scroll to desired element
- Consider a web page which has vertical scroll bar. You need to perform an action on web element which shows up in visible area when user scroll down.
- Webdriver generally auto scrolls to element and perform desired operations.
- Sometimes, webdriver is not able to do so. In this case, you might get element not visible or element is not clickable(if action is click).
- By using explicit wait and fluent wait as mentioned above.
<span class="typ">Actions</span><span class="pln"> actions </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Actions</span><span class="pun">(</span><span class="pln">driver</span><span class="pun">);</span><span class="pln">
actions</span><span class="pun">.</span><span class="pln">moveToElement</span><span class="pun">(</span><span class="pln">element</span><span class="pun">);</span><span class="pln">
actions</span><span class="pun">.</span><span class="pln">perform</span><span class="pun">();</span>
There might be more reasons behind these exceptions which I will add here if I get to know. If you are aware, Share through comment because sharing knowledge is always a good thing.
If you have any doubt, feel free to ask here.
If you like my posts, please like, comment, share and subscribe.
Author: Amod Mahajan
A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.