Site icon

Handle ElementNotVisibleException in Selenium Webdriver

Why we face ElementNotVisibleException ?

There are multiple reasons which may cause this exception:

Case 1 (XPath of WebElements): 

The Xpath you are using for locating a particular web element is correct, but  that is matching with more than one element.

Solutions: 

driver.findElements(By.xpath("locator")).get(0).click();
WebDriverWait w = new WebDriverWait(driver, 20);
WebElement element=w.until(ExpectedConditions.elementToBeClickable(driver.findElement(locator)));
element.click();

Case 2 (Dynamic Loading): 

Selenium Web driver got a feature of performing  auto scroll to a web element and perform desired operations. However, sometimes in case of dynamic loading , you may need to explicitly perform scrolling operations.

Solutions: 

WebElement Element = driver.findElement(By.id("xyz"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();",Element );

Case 3 (AJAX Calls): 

AJAX calls (Asynchronous JavaScript) allows parts of a web page to got refresh, without reloading the entire page. If you want to perform some action on a web element which is still loading because of AJAX calls, then you may get NoSuchElementException or ElementNotclickableException (if performing click operation) or ElementNotVisibleException.
Solutions: 

Exit mobile version