Hello Guys, Welcome to Selenium tutorial. Today we will learn what is Element not visible exception and How to handle element not visible exception in selenium.
Many times I faced this exception and faced issues to find the correct solution. Finally I got so many solutions to fix this exception.
Before moving to element not visible exception, you can read below post that describes the most common exceptions in selenium.
Table of Contents
Element not visible exception is thrown when:
There are many reasons which are mentioned below.
- If we try to locate a web element which is not visible or hidden on screen then we will get the element not visible exception.
- Synchronization problem: If Selenium is faster than application or vice versa.
- If we have used duplicate xpath in our script means more than one web element have same xpath.
- Sometimes weblement is not on the visible area of the screen (means when we scrolled down the screen then element is visible)
How to clear element not visible exception in selenium:
Solution 1: For 1st and 2nd above problem means when element will visible then we will perform operations.
Here we can wait until the element is not visible. Once it will visible then we can perform an action.
Here we can different wait commands.
Thead.sleep(): It is not recommended because we never know in how much time the element will visible on the screen. It might be increasing our test execution.
Implicit wait: It is also not recommended because it will apply whole line of code in the given test script which increase the execution time.
Explicit wait: It is recommended because it provides different expected conditions which we can use till we want to wait.
WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“Locator”)));
Solution 2: Always use the unique path that matches only a single element.[dropshadowbox align=”none” effect=”lifted-both” width=”auto” height=”” background_color=”#ffffff” border_width=”1″ border_color=”#dddddd” ]Note: When xpath is incorrect then selenium throws NoSuchElementException.[/dropshadowbox]
Solution 3: We can use try.. catch block to handle this exception.
Also see this video for more details:
Difference between element not visible and no such element exception:
If element is not visible on the web page or we have given duplicate xpath then it will throw ElementNotVisibleException
If element is not present in the DOM or we are using wrong locator or element is present in any frame then it will throw NoSuchElementException.
How To Fix Element not clickable exception in Selenium:
Finding an element is important in automation testing. You can’t find exact behavior of element on different browser.
In modern application development, developers are using complex approaches to achieve attractive design. It becomes little bit difficult to find element on web pages on different browser.
In this section, we will discuss what element not clickable exception is and how to fix this?
What is element not clickable exception?
When we are trying to click on an element but element is not clickable at that point. Sometimes it is browser specific means on one browser it is working and on other one it is not.
Cause of element not clickable exception:
- On Chrome browser, chrome driver always try to click on middle of the element. In this case if web element is shifted minor then this exception is displayed.
- Overlapping element: In some applications, there are overlapping elements. In this case, when we trying to click on element which is lying below the pop-up then this exception will throw.
- Element is disabled: In some application you will see the element but those are not enabled. Sometimes developers disabled submit button until all date are not filled in the form and in the testing we trying to click on submit button without fill all fields then this exception is displayed.
Fixing element not clickable exception:
- Maximize the browser: If you are using coordinates to click on element it is better to maximize the browser otherwise it will click on wrong element.
- Add waits commands: If you are using dynamic application, sometimes page is loaded but in the DOM web elements are still loading. In this case, you should use wait commands to wait certain period of time and trying to click on web element.
- Use Coordinates of element: In complex applications where might be nested elements, finding and selecting the web element is difficult. In such cases use element coordinates to find and click on that.
Element Not selectable exception in Selenium
This exception exists into InvalidElementStateException class.
This exception occurs when element is present on web page but can not be selectable.
Fixing Element Not Selectable exception:
We can use wait command to wait until element becomes clickable.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); wait.Until(ExpectedConditions. elementToBeClickable(By.id(”City_dropdown”));
We have now learnt about ‘Element not visible exception’ and how to fix this. I have also mentioned the common causes of this exception.
We have also learnt about difference between element not visible and no such element exception. In the last we learnt about ‘element not clickable exception’, causes of this exception and how to fix this exception.
If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below.