The most common exceptions and their solutions:
Noalertpresentexception
Indicates that a user had tried to access the alert when one was not present.
((Javascriptexecutor) driver). Executescript ("alert ()");
webdriverwait wait = new Webdriverwait (Driver.getdriver (), 15);
Alert alert = Wait.until (Expectedconditions.alertispresent ());
Alert.accept ();
More methods to work with alerts here.
Nosuchwindowexception
Thrown by Webdriver.switchto (). Window (String windowname).
If you get the IT in IE. Try to specify
Capabilities.setcapability (internetexplorerdriver.introduce_flakiness_by_ignoring_security_domains,true);
Or you'll have the to switch to another window using correct method.
Nosuchelementexception
Thrown by Webdriver.findelement, and Webelement.findelement (by).
Sometimes it happens when your element was located in another iframe. And you'll need to first switch to the IFRAME element, that contains the element, that's want to interact with:
Driver.switchto (). frame ();
eg
Driver.switchto (). FRAME (1); Driver.switchto (). FRAME (Driver.findelement (by.id ("id")));
When your ' ve finished interacting with the elements within the frame, you'll be need to switch back to the main webpage.
Driver.switchto (). Defaultcontent ();
Or you'll have a to-wait for your element would be clickable:
/**
* Elementtobeclickable (webelement Element)
*an expectation for checking a element is visible and enabled such so you can click it.
*/
Protected Boolean validateelementclickable (final by) {
try{
New Webdriverwait (Driver.getdriver (), (). Until (Expectedconditions.elementtobeclickable (by));
}catch (RuntimeException e) {
return false;
}
return true;
}
Elementnotvisibleexception:element not visible
You'll have a to-wait for your element would be visible:
Public Boolean validateelementclickable (final by) {
try{
New webdriverwait (Driver,). Until (Expectedconditions.elementtobeclickable (by));
}catch (RuntimeException e) {
return false;
}
return true;
}
Stale Element Reference Exception
A stale Element Reference exception is thrown in one of the both cases, the first being more common than the second:
The element has been deleted entirely.
The most frequent cause of the-is, and the element is a part of have been refreshed, or the user has navigated AW Ay to another page. The If the element has been replaced with an identical one, a useful strategy are to look up the element again.
The element is no longer attachedto the DOM. It's entirely possible that your code might has a reference to an E Lement that's no longer attached to the DOM. You should discard the current reference your hold and replace it, possibly by locating the element again once it's attach Ed to the DOM.
To solve it-can wait for your element become visible:
Public Boolean validateelementvisible (final by) {
try{
New webdriverwait (Driver,). Until (expectedconditions.visibilityofelementlocated (by));
}catch (RuntimeException e) {
return false;
}
return true;
}
Seleniumwatchdog Destroyharder
Instead of Driver.quit (); Try
Driver.close ();
Runtime.getruntime (). EXEC ("taskkill/f/im chromedriver.exe");
Runtime.getruntime (). EXEC ("taskkill/f/im firefox.exe");
Driver.quit ();
Close ()-It is used to close the browser or page currently which are having the focus.
Quit ()-It is used to shut the Web driver instance
If you get the exception like "Org.openqa.selenium. webdriverexception: Unknown error:unhandled Inspector Error: {"Code": -32603, "message": "cannot navigate To invalid URL"}"
That means, specified the incorrect URL like (without protocol maybe) like:
Driver.get ("www.google.com");
You'll have the to change it to:
Driver.get ("https://google.com");
Unreachablebrowserexception
Indicates there is a problem communicating with the browser being controlled or the Selenium server. The most common causes for this exception is:
- The provided server address to Remotewebdriver are invalid, so the connection could isn't be established.
- The browser has died mid-test.
Check the latest version of Webdriver http://www.seleniumhq.org/download/
Check where you use Drver.close ();
Check if the "close" comes before the @AfterClass is called.
Unhandledalertexception
That's means that your test faced unpredictable Alert.
You'll have the to handle it using special methods below.
Some useful methods for alerts:
Protected String Getalerttext () {
Get a handle to the open alert, prompt or confirmation
Alert alert = Driver.getdriver (). SwitchTo (). alert ();
Get the text of the alert or prompt
return Alert.gettext ();
}
public void Clickokinalert () {
Assert.asserttrue (Validatealertpresent (), "the alert is not present");
Get a handle to the open alert, prompt or confirmation
Alert alert = Driver.getdriver (). SwitchTo (). alert ();
Get the text of the alert or prompt
System.out.println ("alert:" + alert.gettext ());
and acknowledge the alert (equivalent to clicking "OK")
Alert.accept ();
}
public Boolean validatealertpresent () {
try{
New webdriverwait (Driver,). Until (Expectedconditions.alertispresent ());
}catch (RuntimeException e) {
return false;
}
return true;
}
Webdriverexception:unknown Error:element is not clickable on point
Try to use instead of driver.findelement (by). Click () Use:
actions = new actions (driver);
Actions.click (Driver.findelement (by)). Build (). Perform ();
Timed out receiving message from renderer
Try to use older version of Chromedriver (for example instead of using 15th use 12th)
How to handle your webdriver exceptions