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
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 🙂
Avinash says
Nice one, Thanks for clearly presenting with examples and explanation
Mukesh Otwani says
You’re welcome…:)
vivek says
Really great content up to point not only theoretical.
Mukesh Otwani says
Thanks, Vivek…:)
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?
Mukesh Otwani says
Hi Mahesh, Sorry question is not clear,explain in brief please
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)
Mukesh Otwani says
Hey Mahesh,
below article will help you.
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
Mukesh Otwani says
Hi Mahesh,
use explicit wait for above error.
sa says
Hello sir
If we click on a element(Enter button) Sometimes Dragging and sometimes it is throwing webdriver exception
Mukesh Otwani says
Hi Sagar,
Any example for this?
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.
Mukesh Otwani says
Hi Maheshwari,
Thanks a ton.
NullPointerException- When any variable point to null
IOException- Whenever we deal with files this checked exception will come.
Senthil says
The Use getAttribute() command is finding tooltip text… Thanks
Mukesh Otwani says
Great Senthil 🙂 I will try too soon.
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
Mukesh Otwani says
Hi Senthil,
Could me please explain in using url as well?
I hope you have solved it if not then let me know.
Satya says
Hi
Please share a post regarding how to solve class not found exception.
Mukesh Otwani says
Hi Satya,
Can u explain in brief?
Kannan says
Good post. Got some better understanding about exception handling 🙂
Mukesh Otwani says
Thanks Kannan 🙂
priti says
hi mukesh
I want to know how to Read ToolTip text in in Selenium-WebDriver
Mukesh Otwani says
Hi Priti,
use mouse hover and use getText ()