Selenium Exception-NoSuchElementException

NoSuchElementException : This Exception Produce when FindBy method can’t find the element.




Hierarchy of  NoSuchElementException :
·         java.lang.Object
·         java.lang.Throwable
·         java.lang.Exception
·         java.lang.RuntimeException
·         org.openqa.selenium.NoSuchElementException


                                             


Reasons of NoSuchElementException:

  • We  specified wrong  Locator(ID, Name, Link Text, CSS Selector, DOM (Document  Object Model), XPath etc.) for the element.
  • The element is not on the page.          
  • We are not waiting enough on the page for that element to load





A).Locator is wrong and element is not on the page.


Example1 : Here we are generating  NoSuchElementException   through  wrong  xpath:

Actual  XPath of  “ Older Post “ link- .//*[@id='Blog1_blog-pager-older-link']
We are putting wrong xpath   “.//*[@id='Blog11_blog-pager-older-link']”  in below example


package example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NoSuchElementException_Example
{
      WebDriver driver;
     
      @BeforeTest
      public void beforeTest()
      { //Set Path of the latest ChromeDriver.exe file
      System.setProperty("webdriver.chrome.driver","E:\\Jars\\chromedriver.exe");
      //Create the Object of chrome driver
      driver = new ChromeDriver();
      //maximize the browser window
      driver.manage().window().maximize();
      String url="http://selenium-code.blogspot.in/2017/08/selenium-exception-      
      nosuchelementexcepti.html";
      //Open URL in Web Browser
      driver.get(url);
      }
     
      //set Test Priority to execution
      @Test(priority=1)
      public void test() throws InterruptedException
      {   //apply static wait of 5000 millisecond 
            Thread.sleep(5000);
            // find the element "Older Post" through xpath locator and create link type object of     
             WebElement
             WebElement link= driver.findElement(By.xpath("//*[@id='Blog11_blog-pager-older-
             link']"));
              //click on link
              link.click();
           
       }
     
      @AfterTest
      public void aftertest()
      {   //Close all the instance of  browser                                                                                                                            
            driver.quit();
      }
     

}



Output-With Wrong Xpath
Starting ChromeDriver 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41) on port 17423
Only local connections are allowed.
Aug 05, 2017 5:24:56 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
FAILED: test
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='Blog11_blog-pager-older-link']"}
  (Session info: chrome=59.0.3071.115)







Example 2:


package example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NoSuchElementException_Example
{ WebDriver driver;
     
      @BeforeTest
      public void beforeTest()
      { //Set Path of the latest ChromeDriver.exe file
      System.setProperty("webdriver.chrome.driver","E:\\Jars\\chromedriver.exe");
      //Create the Object of chrome driver
      driver = new ChromeDriver();
      //maximize the browser window
      driver.manage().window().maximize();
      String url="http://selenium-code.blogspot.in/2017/08/selenium-exception-
      nosuchelementexcepti.html";
      //Open URL in Web Browser
      driver.get(url);
      }
     
      //set Test Priority to execution
      @Test(priority=1)
      public void test() throws InterruptedException
      {   //apply static wait of 5000 millisecond 
            Thread.sleep(3000);
            // find the element "Older Post" through xpath locator and create link type object of 
            WebElement
            WebElement link= driver.findElement(By.xpath("//*[@id='Blog1_blog-pager-older-
            link']"));
            //click on link
            link.click();
            Thread.sleep(2000);

           
      }
     
      @AfterTest
      public void aftertest()
      {   //Close all the instance of browser                                                                                                                           
            driver.quit();
      }
     

}


Output-(with Right Xpath)
Starting ChromeDriver 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41) on port 16277
Only local connections are allowed.
Aug 05, 2017 5:33:19 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
PASSED: test

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================




B). We are not waiting enough on the page for that element to load:

·         1) The page is still being rendered and  we have  already finished our  element search then we obtain  NoSuchElementException.


How Can Handle this type of Situation :

We can apply below codes to remove this exception condition
1.      By applying  WebDriverWait, webdriver object  wait for a specific time (in second) of an    element for its visibility.
              
              WebDriverWait wait = new WebDriverWait(driver, 10);       
               wait.until(ExpectedConditions.visibilityOf(link));
  
 

2.  We can handle NoSuchElementException through try-catch block inside Generic method
                
    
         public boolean isElementPresent(By by) {
         boolean isPresent = true;
         try {
         driver.findElement(by);
         } catch (NoSuchElementException e) {
          isPresent = false;
         }
        return isPresent
        }



Example 3:
package example;

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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NoSuchElementException_Example
{ WebDriver driver;
     
      @BeforeTest
      public void beforeTest()
      { //Set Path of the latest ChromeDriver.exe file
      System.setProperty("webdriver.chrome.driver","E:\\Jars\\chromedriver.exe");
      //Create the Object of chrome driver
      driver = new ChromeDriver();
      //maximize the browser window
      driver.manage().window().maximize();
      String url="http://selenium-code.blogspot.in/2017/08/selenium-exception-
       nosuchelementexcepti.html";
      //Open URL in Web Browser
      driver.get(url);
      }
     
      //set Test Priority to execution
      @Test(priority=1)
      public void test() throws InterruptedException
      {   //apply static wait of 5000 millisecond 
            Thread.sleep(3000);
            // find the element "Older Post" through xpath locator and create link type object of      
            WebElement
             WebElement link= driver.findElement(By.xpath("//*[@id='Blog1_blog-pager-older-
             link']"));
             // Pass time in second (10) to wait of element on screen
             WebDriverWait wait = new WebDriverWait(driver, 10);
             wait.until(ExpectedConditions.visibilityOf(link));
             System.out.println("Apply wait of 10 second for visibility of Element");
        
             //click on link
              link.click();
             Thread.sleep(2000);
           
           
      }
     
      @AfterTest
      public void aftertest()
      {   //Close all the instance of browser                                                                                                                           
            driver.quit();
      }
     

}







OutPut—With WebDriverWait


Starting ChromeDriver 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41) on port 12756
Only local connections are allowed.
Aug 05, 2017 6:29:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Apply wait of 10 second for visibility of Element
PASSED: test

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================







Post a Comment

0 Comments