Understanding common selenium exceptions.

In this post will try to simulate the common selenium exceptions . Also, for better understanding, why we might get these exceptions during our tests, we will look at code snippets for clarity and their run outputs.

NoSuchWindowException

When we try to switch to an window which is not present gives us this exception as shown in the below example.

Once you open Microsoft Bing search open page there would be no window as mentioned below.

@Test
public void sampleTest() throws MalformedURLException
{
driver.get("https://www.bing.com");
driver.switchTo().window("NoWindowWithThisName");
driver.close();
}

 

Run output of the test method:

FAILED: sampleTest
org.openqa.selenium.NoSuchWindowException: Unable to locate window “NoWindowWithThisName”.

The above code snippet throws us an exception, as we are trying to switch to an window that is not present.

SeleniumNoSuchWindowExample

 

NoSuchFrameException

Now we try to simulate no such frame exception. Usually, we get this when we try to switch to an frame that is not present.
Also, this is similar to window exception, we encounter frame exception while switching to frame that is not present.

@Test
public void sampleTest() 
{
driver.get("https://www.bing.com");
driver.switchTo().frame("NoFrameWithThisName");
driver.close();
}

 

Run output:

FAILED: sampleTest
org.openqa.selenium.NoSuchFrameException: No frame element found by name or id NoFrameWithThisName

SessionNotFoundException

This exception in encountered, when we try to access the Remote webdriver instance after calling the quit(0 method.Below is sample snippet :

Mostly, we use remote web driver while testing using selenium grid or cloud testing.

@Test
public void sampleTest() 
{
driver.get("https://www.bing.com");
driver.quit();
driver.get("https://www.bing.com");
}

 

Run output:

org.openqa.selenium.remote.SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()?

NoAlertPresentException

@Test
public void sampleTest() 
{
try{
    driver.switchTo().alert().accept();
  }
    catch (NoAlertPresentException nape){		
             nape.printStackTrace();	}
}

 

Run output:

org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information)

We get this exception when we try to perform an action i.e., either accept() or dismiss() when there is no alert present.

NotConnectedException

Caused by: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:

We get this exception, when Firefox is not connected. Also, the communication between the web driver tests and the Firefox browser is somehow broken. Furthermore, this happens when we are trying to run on machines with firewall enabled , port blocked by anti virus programs.Make sure we use compatible Firefox browser binaries.If we encounter this exception try to find root cause by investigating one cause at the time.