Basic Selenium

Exceptions in Selenium WebDriver

Before moving to Runtime Exception, you should be aware of Exception meaning.

Exception general meaning a condition that is not common, in Exceptions in Selenium WebDriver occurs many times in different scenarios let’s have a look to all exception one by one.

 

Exceptions in Selenium WebDriver

Exceptions in Selenium WebDriver

 

Exceptions in Selenium WebDriver

NoSuchElementException

This exception occurs when WebDriver is unable to identify the elements during run time. Due to wrong selector or selector, which is, not exist.
Example:-

driver.findElement(By.id("invalidid")).sendKeys("Mukesh");

 

ElementNotVisibleException

This Exception occurs when the element presence is in DOM, it is not visible.
Example:-
Hidden Elements, which has presence in DOM and it, is not visible. Visibility means the height and
width should be greater than zero. Hidden Elements are defined in HTML using of type=”hidden”.

driver.findElement(By.id("hiddenid")).sendKeys("Mukesh");

 

NoSuchFrameException

This Exception occurs when the driver is switching to an invalid frame, which is not available.
Example:-

driver.switchTo().frame(invalidindex);
(or)
driver.switchTo().frame("frame_z");

 

//frame_z is the name of the invalid frame
For frames indexing starts from Zero. Try to access the frame by providing invalid index.

NoAlertPresentException

This Exception occurs when the driver is switching to an invalid Alert, which is not available.
Example:-

driver.switchTo().alert().accept();

//Execute this command on browser without invoking the alert.

NoSuchWindowException

This Exception occurs when the driver is switching to an invalid Window, which is not available.
Example:-

driver.switchTo().window("invalidwindowname");

 

WebDriverException

This Exception occurs when the driver is performing the action after immediately closing the browser.
Example:-

driver.close();
driver.findElement(By.id("username")).sendKeys("Mukesh");

 

SessionNotFoundException

This Exception occurs when the driver is performing the action after immediately quitting the browser.
Example:-

driver.quit();
driver.findElement(By.id("username")).sendKeys("Mukesh");

 

StaleElementReferenceException

This Exception occurs when the Element belongs to a different frame than the current one. The user has navigated away to another page.

Example:-
WebElement element=driver.findElement(By.id("username"));// Element is available in parent window
driver.switchTo().window(Child_Window);//Switch to Child Window
element.sendKeys("Mukesh");//perform the action on the element which is not visible in the child window

Check out more exceptions here.

Thanks for visiting my blog. Please leave a comment if you have any issues in Selenium.

Keep in touch. Have a nice day 🙂

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

24 thoughts on “Exceptions in Selenium WebDriver

  1. Avinash says:

    Nice one, Thanks for clearly presenting with examples and explanation

    1. You’re welcome…:)

  2. vivek says:

    Really great content up to point not only theoretical.

  3. Mahesh Chhetri says:

    When i try use page system test, it has to return two data, one is return data and another is to return link for another page for testing keep continue. it has problem with this. can it be solve?

    1. Hi Mahesh, Sorry question is not clear,explain in brief please

      1. Mahesh Chhetri says:

        I have a form
        and fileds name are

        firstName, Lastname, email, phone, cardnumber, month, year, cvv, address, city, state, zip

        When i test, it fills the field till “cvv” only and then this error comes and stop the test

        the error is

        Failed: element not visible
        (Session info: chrome=50.0.2661.102)
        (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86)

        1. Hey Mahesh,

          below article will help you.

  4. Mahesh says:

    Hi priti,

    ” Failed: element not visible
    (Session info: chrome=50.0.2661.102)
    (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed09 83e7c4),platform=Windows NT 6.1 SP1 x86)

    Above this error comes while e2e testing, All the input, select testing well, but suddenly above error comes when it has to fill the data into another input box in the same form. how it can be solve. please

    1. Hi Mahesh,

      use explicit wait for above error.

  5. sa says:

    Hello sir
    If we click on a element(Enter button) Sometimes Dragging and sometimes it is throwing webdriver exception

    1. Hi Sagar,

      Any example for this?

  6. Maheshwari B says:

    Hi Mukesh,

    you have explained Runtime exceptions very nicely, it was very useful for me.
    But wanted to know details on other exceptions like nullpointerException, IOException etc.

    1. Hi Maheshwari,

      Thanks a ton.

      NullPointerException- When any variable point to null
      IOException- Whenever we deal with files this checked exception will come.

  7. Senthil says:

    The Use getAttribute() command is finding tooltip text… Thanks

    1. Great Senthil 🙂 I will try too soon.

  8. Senthil says:

    Hi,

    How to update quantity in the cart items. Because we have added 2 to 3 products in the cart items, when changing the quantity all 3 products quantity element name is same. how to get element those case?

    Thanks
    Senthil

    1. Hi Senthil,

      Could me please explain in using url as well?

      I hope you have solved it if not then let me know.

  9. Satya says:

    Hi

    Please share a post regarding how to solve class not found exception.

    1. Hi Satya,

      Can u explain in brief?

  10. Kannan says:

    Good post. Got some better understanding about exception handling 🙂

  11. priti says:

    hi mukesh
    I want to know how to Read ToolTip text in in Selenium-WebDriver

    1. Hi Priti,

      use mouse hover and use getText ()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.