Site icon Learn Automation

Element Not Visible Exception in Selenium

Element not visible exception in selenium

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.

Most Common Exceptions in Selenium

Element not visible exception is thrown when:

There are many reasons which are mentioned below.

  1. 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.
  2. Synchronization problem: If Selenium is faster than application or vice versa.
  3. If we have used duplicate xpath in our script means more than one web element have same xpath.
  4. 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.

Syntax:

Thread.sleep(30);

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.

Syntax:

driver.manage().timeouts().implicitlywait(20, TimeUnit.SECONDS);

Explicit wait: It is recommended because it provides different expected conditions which we can use till we want to wait.

Syntax:

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“Locator”)));

In case of element is not in visibility area of the screen then first we need to scroll the web page using javaScriptExecutor then perform an action.

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", element);

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.

Handle Using try catch block

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:

  1. 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.
  2. 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.
  3. 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:

 


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.

Syntax:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions. elementToBeClickable(By.id(”City_dropdown”));

Summary

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.

Exit mobile version