Man must explore, and this is exploration at its greatest

Problems look mighty small from 150 miles up

Posted by Start Bootstrap on August 24, 2022

Exceptions

ElementClickInterceptedException

                                
Package org.openqa.selenium
Class ElementClickInterceptedException

    java.lang.Object
        java.lang.Throwable
            java.lang.Exception
                java.lang.RuntimeException
                    org.openqa.selenium.WebDriverException
                        org.openqa.selenium.InvalidElementStateException
                            org.openqa.selenium.ElementNotInteractableException
                                org.openqa.selenium.ElementClickInterceptedException 
                                
                            

Indicates that a click could not be properly executed because the target element was obscured in some way.

ElementClickInterceptedException occurs when the target element that you want to click is overlaid by some other element in the web page.

ElementClickInterceptedException

In the above image whenever i tried to click on the Book Store Application tab i will get ElementClickInterceptedException because of that below add. The add covers the above tab.

                              
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ElementClickInterceptedExceptionExample1 {

    public static void main(String[] args) {
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/");
        WebElement bookStoreApplicationBtn = driver.findElement(By.xpath("//*[text()='Book Store Application']"));
        bookStoreApplicationBtn.click();
    }

}
                              
                            


ElementClickInterceptedException

We can Overcome this problem by using JavaScriptExecutor

                              
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ElementClickInterceptedExceptionExample1 {

    public static void main(String[] args) {
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/");
        WebElement bookStoreApplicationBtn = driver.findElement(By.xpath("//*[text()='Book Store Application']"));
        JavascriptExecutor jse = ((JavascriptExecutor)driver);
        jse.executeScript("arguments[0].click()", bookStoreApplicationBtn);
        driver.quit();
    }

}
                                
                            



                              
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ElementClickInterceptedExceptionExample1 {

    public static void main(String[] args) {
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/");
        WebElement bookStoreApplicationBtn = driver.findElement(By.xpath("//*[text()='Book Store Application']"));
        JavascriptExecutor jse = ((JavascriptExecutor)driver);
        jse.executeScript("arguments[0].scrollIntoView()", bookStoreApplicationBtn);
        bookStoreApplicationBtn.click();
        driver.quit();
    }

}
                              
                            

ElementNotInteractableException

Thrown to indicate that although a WebElement is present on the DOM, it is not in a state that can be interacted with. This includes an element that is not displayed or whose center point can not be scrolled into the viewport.

                                
Package org.openqa.selenium
Class ElementNotInteractableException

    java.lang.Object
        java.lang.Throwable
            java.lang.Exception
                java.lang.RuntimeException
                    org.openqa.selenium.WebDriverException
                        org.openqa.selenium.InvalidElementStateException
                            org.openqa.selenium.ElementNotInteractableException
                                
                            

For This Exception we can follow the above methods.

InvalidElementStateException

                                
Package org.openqa.selenium
Class InvalidElementStateException

    java.lang.Object
        java.lang.Throwable
            java.lang.Exception
                java.lang.RuntimeException
                    org.openqa.selenium.WebDriverException
                        org.openqa.selenium.InvalidElementStateException 
                                
                            

Indicates that a WebElement is in a state that means actions cannot be performed with it. For example, attempting to clear an element that isn’t both editable and resettable.

InvalidElementStateException

In the above ficture there is Click Me button when ever i tried to use clear method instead of click method we will get InvalidElementStateException.

                              
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample3 {

    public static void main(String[] args) {
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/buttons");
        WebElement interactionsBtn = driver.findElement(By.xpath("//button[text()='Click Me']"));
        interactionsBtn.clear();
        driver.quit();
    }

}
                              
                            
InvalidElementStateException
                              
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample3 {

    public static void main(String[] args) {
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/buttons");
        WebElement interactionsBtn = driver.findElement(By.xpath("//button[text()='Click Me']"));
        interactionsBtn.click();
        driver.quit();
    }

}
                              
                            
InvalidElementStateException

When we try to perform sendKeys on password field then it will throw InvalidElementStateException in Selenium Webdriver.

                              
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample4 {

    public static void main(String[] args) throws Exception {
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://seleniumpractise.blogspot.com/2016/09/how-to-work-with-disable-textbox-or.html");
        WebElement interactionsBtn = driver.findElement(By.id("pass"));
        interactionsBtn.sendKeys("password123");
        driver.quit();
    }

}
                              
                            

The solution javascript

                              
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample4 {

    public static void main(String[] args) throws Exception {
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://seleniumpractise.blogspot.com/2016/09/how-to-work-with-disable-textbox-or.html");

        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("document.getElementById('pass').value = 'password123';");
        
        Thread.sleep(2000);
        
        driver.quit();
    }

}
                              
                            


                              
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample4 {

    public static void main(String[] args) throws Exception {
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://seleniumpractise.blogspot.com/2016/09/how-to-work-with-disable-textbox-or.html");
        WebElement interactionsBtn = driver.findElement(By.id("pass"));
        Select select = new Select(interactionsBtn);
        select.selectByIndex(0);

        Thread.sleep(2000);
        
        driver.quit();
    }

}
                              
                            
UnexpectedTagNameException

NoAlertPresentException

                                
Package org.openqa.selenium
Class NoAlertPresentException

    java.lang.Object
        java.lang.Throwable
            java.lang.Exception
                java.lang.RuntimeException
                    org.openqa.selenium.WebDriverException
                        org.openqa.selenium.NotFoundException
                            org.openqa.selenium.NoAlertPresentException 
                                
                            

Indicates that a user has tried to access an alert when one is not present.

NoAlertPresentException

In the above page there is no alert but i going accept the alert button

                              
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample5 {

    public static void main(String[] args){
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3schools.com/");
        driver.switchTo().alert().accept();
        driver.quit();
    }
}
                              
                            
NoAlertPresentException
NoAlertPresentException

In the above picture there is second click me button when we click that button alert will apper after some time but whever i tried to accet ommediately we will get get error.

                            
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample6 {

    public static void main(String[] args){
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/alerts");
        driver.findElement(By.id("timerAlertButton")).click();
        driver.switchTo().alert().accept();
        driver.quit();
    }

}
                            
                        
NoAlertPresentException
                              
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample6 {

    public static void main(String[] args){
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/alerts");
        driver.findElement(By.id("timerAlertButton")).click();
        new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
        driver.switchTo().alert().accept();
        driver.quit();
    }

}
                              
                            

NoSuchFrameException

Thrown by WebDriver.switchTo().frame(int frameIndex) and WebDriver.switchTo().frame(String frameName).

HTML Iframes

An HTML iframe is used to display a web page within a web page.

NoSuchFrameException

In the above picture there is try it button now iam going click that button.

                              
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample7 {

    public static void main(String[] args){
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert");
        driver.findElement(By.xpath("//button[text()='Try it']")).click();
        new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
        driver.switchTo().alert().accept();
        driver.quit();
    }

}
                              
                            
NoSuchElementException

Even the button xpath is correct i got NoSuchElementException why because element is inside the frame first we have to move insde the frame then click.

If you enter a wrong frame name or id element you will get NoSuchFrameException

check the above image frame name is "iframeResult" but purposely iam enetring wrong name

                              
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample7 {

    public static void main(String[] args){
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert");
        driver.switchTo().frame("iframeResult2");//Wring frame name
        driver.findElement(By.xpath("//button[text()='Try it']")).click();
        new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
        driver.switchTo().alert().accept();
        driver.quit();
    }

}
                              
                            
NoSuchFrameException
                              
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample7 {

    public static void main(String[] args){
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert");
        driver.switchTo().frame("iframeResult");//correct frame name
        driver.findElement(By.xpath("//button[text()='Try it']")).click();
        new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
        driver.switchTo().alert().accept();
        driver.quit();
    }

}
                              
                            
NoSuchFrameException

If you want het h1 tag text from the above screen it inside in nested frame so we have to switch two frames

                              
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample7 {

    public static void main(String[] args){
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml_iframe_height_width");
        driver.switchTo().frame("iframeResult");//outer frame
        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@src='demo_iframe.htm']")));//inner frame
        System.out.println(driver.findElement(By.xpath("//h1[text()='This page is displayed in an iframe']")).getText());

        driver.quit();
    }

}
                              
                            

NoSuchSessionException

Thrown by any command being called after WebDriver.quit().

                                
Package org.openqa.selenium
Class NoSuchSessionException

    java.lang.Object
        java.lang.Throwable
            java.lang.Exception
                java.lang.RuntimeException
                    org.openqa.selenium.WebDriverException
                        org.openqa.selenium.NoSuchSessionException
                                 
                            
                              
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample7 {

    public static void main(String[] args){
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3schools.com/");
        driver.quit();
        driver.getCurrentUrl(); //driver already closed
    }

}
                              
                            
NoSuchSessionException

NoSuchWindowException

                                
Package org.openqa.selenium
Class NoSuchWindowException

    java.lang.Object
        java.lang.Throwable
            java.lang.Exception
                java.lang.RuntimeException
                    org.openqa.selenium.WebDriverException
                        org.openqa.selenium.NotFoundException
                            org.openqa.selenium.NoSuchWindowException
                                 
                            

Thrown by WebDriver.switchTo().window(String windowName).

If you are to swith thw windo which is not available or wrong window/tab you will get NoSuchWindowException

                              
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample8 {

    public static void main(String[] args){
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3schools.com/");
        driver.switchTo().window("solomonwindow"); //wrong windo name
        driver.quit();
    }

}
                              
                            
NoSuchWindowException

StaleElementReferenceException

                                
Package org.openqa.selenium
Class StaleElementReferenceException

    java.lang.Object
        java.lang.Throwable
            java.lang.Exception
                java.lang.RuntimeException
                    org.openqa.selenium.WebDriverException
                        org.openqa.selenium.StaleElementReferenceException
                                 
                            

Indicates that a reference to an element is now "stale" --- the element no longer appears on the DOM of the page.

Stale means old, decayed, no longer fresh. Stale Element means an old element or no longer available element. Assume there is an element that is found on a web page referenced as a WebElement in WebDriver. If the DOM changes then the WebElement goes stale. If we try to interact with an element which is staled then the StaleElementReferenceException is thrown.

Common Causes

A stale element reference exception is thrown in one of two cases, the first being more common than the second:

  • The element has been deleted entirely.
  • The element is no longer attached to the DOM.

The Element has been deleted

The most frequent cause of this is that page that the element was part of has been refreshed, or the user has navigated away to another page. A less common, but still common cause is where a JS library has deleted an element and replaced it with one with the same ID or attributes. In this case, although the replacement elements may look identical they are different; the driver has no way to determine that the replacements are actually what's expected.

If the element has been replaced with an identical one, a useful strategy is to look up the element again. If you do this automatically, be aware that you may well be opening your tests to a race condition and potential flakiness. For example, given the code:

                              
WebElement element = driver.findElement(By.id("example"));
String text = element.getText();
                              
                            

If element.getText returns before the element is removed from the DOM you'll get one result. If, however, the element is removed from the DOM and your code does an automatic lookup for the element again before element.getText a different result may be returned.

The Element is not Attached to the DOM

A common technique used for simulating a tabbed UI in a web app is to prepare DIVs for each tab, but only attach one at a time, storing the rest in variables. In this case, it's entirely possible that your code might have a reference to an element that is no longer attached to the DOM (that is, that has an ancestor which is document.documentElement).

If WebDriver throws a stale element exception in this case, even though the element still exists, the reference is lost. You should discard the current reference you hold and replace it, possibly by locating the element again once it is attached to the DOM.

How To Overcome Stale Element Reference Exception in Selenium:

Solution 1: Refreshing the web page
                              
driver.navigate().refersh();
driver.findElement(By.xpath("xpath here")).click();
                              
                            
Solution 2: Using Try Catch Block
                              
// Using for loop, it tries for 3 times. 
// If the element is located for the first time then it breaks from the for loop nad comeout of the loop
for(int i=0; i<=2;i++){
  try{
     driver.findElement(By.xpath("xpath here")).click();
     break;
  }
  catch(Exception e){
     Sysout(e.getMessage());
  }
}
                              
                            
Solution 3: Using ExpectedConditions.refreshed

Wait for the element till it gets available

                                
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("table")));
                                
                              

Use ExpectedConditions.refreshed to avoid StaleElementReferenceException and retrieve the element again. This method updates the element by redrawing it and we can access the referenced element.

                                
wait.until(ExpectedConditions.refreshed(ExpectedConditions.stalenessOf("table")));
                                
                              
Solution 4: Using POM

We can handle Stale Element Reference Exception by using POM.

We could avoid StaleElementException using POM. In POM, we use initElements() method which loads the element but it won’t initialize elements. initElements() takes latest address. It initializes during run time when we try to perform any action on an element. This process is also known as Lazy Initialization.

                                
public boolean retryingFindClick(By by) {
  boolean result = false;
  int attempts = 0;
  while(attempts < 2) {
      try {
        driver.findElement(by).click();
        result = true;
        break;
      } catch(StaleElementException e) {
      }
      attempts++;
  }
  return result;
}
                                
                              
                                
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample9 {

    public static void main(String[] args){
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3schools.com/");
        WebElement w3loginbtn = driver.findElement(By.id("w3loginbtn"));
        System.out.println(w3loginbtn.getText());
        driver.navigate().refresh();
        System.out.println(w3loginbtn.getText());
        driver.quit();
    }

}
                                
                              
StaleElementReferenceException
                                
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExceptionExample9 {

    public static void main(String[] args){
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3schools.com/");
        WebElement w3loginbtn;
        w3loginbtn = driver.findElement(By.id("w3loginbtn"));
        System.out.println(w3loginbtn.getText());
        driver.navigate().refresh();
        try {
            System.out.println(w3loginbtn.getText());
        }catch (StaleElementReferenceException e) {
            w3loginbtn = driver.findElement(By.id("w3loginbtn"));
            System.out.println(w3loginbtn.getText());
        }
        driver.quit();
    }

}