Hands-on with different exceptions in Selenium and Java. Please go through the different scenario’s of Exceptions with Programs,Exceptions and Solution.
Run-Time Exception: org.openqa.selenium.InvalidSelectorException
1 2 3 4 5 6 7 8 9 |
public class Facebook { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "geckodriver.exe"); WebDriver driver = newFirefoxDriver(); driver.get("http://www.facebook.com"); String value = driver.findElement(By.xpath("//span[text()='Create an account'")).getText(); System.out.println(value); } } |
Console Output:
1 2 3 4 5 |
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: Given xpath expression "//span[text()='Create an account'" is invalid: SyntaxError: The expression is not a legal expression. at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:311) at Facebook.main(Facebook.java:12) |
Solution:
1 2 3 |
This exception is occurred due to the syntax error in Xpath. Update the Xpath mentioned below: //span[text()=’Create an acount’”] |
Run-Time Exception: org.openqa.selenium.StaleElementReferenceException Indicates that a reference to an element is now “stale” — the element no longer appears on the DOM of the page.
1 2 3 4 5 6 7 8 9 10 11 |
public class Facebook { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "geckodriver.exe"); WebDriver driver = newFirefoxDriver(); driver.get("http://www.facebook.com"); List<WebElement>inputList=driver.findElements(By.xpath("//input[@type='text']")); System.out.println("Number of input Elements:" + inputList.size()); driver.navigate().refresh();//Refresh the Page inputList.get(0).sendKeys("Fname"); } } |
Console Output:
1 2 3 4 5 6 |
Number of input Elements:5 Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: The element reference of <input id="u_0_j" class="inputtext _58mg _5dba _2ph-" name="firstname" type="text"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed |
Solution:
1 2 3 4 5 6 7 8 |
In Selenium when the page is getting refreshed, the existing elements in the DOM removed and new elements are added. All the refernces to the previous elements are lost. We should try to find again once the refresh is done. driver.navigate().refresh(); inputList = driver.findElements(By.xpath("//input[@type='text']")); inputList.get(0).sendKeys("Fname"); |
Run-Time Exception: org.openqa.selenium.NoSuchElementException
1 2 3 4 5 6 7 8 |
public class Facebook { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "geckodriver.exe"); WebDriver driver = newFirefoxDriver(); driver.get("http://www.facebook.com"); driver.findElement(By.id("invalid element id")).sendKeys("Selenium"); } } |
Console Output:
1 2 3 4 |
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: #invalidid org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:311) at Facebook.main(Facebook.java:15) |
Solution:
1 2 |
Provide the property value which is valid. driver.findElement(By.id(“<valid element id>”)).sendKeys(“Selenium”); |
Run-Time Exception:org.openqa.selenium.ElementNotInteractableException
1 2 3 4 5 6 7 |
public class Facebook { public static void main(String[] args) { WebDriver driver = newFirefoxDriver(); driver.get("http://www.facebook.com"); driver.findElement(By.tagName("input")).sendKeys("total-qa"); } } |
Console Output:
1 2 |
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <input name="lsd" type="hidden"> is not reachable by keyboard |
Solution:
1 2 3 |
The first input element available in the Facebook is hidden. So, interacting with an hidden element throws the “ElementnotInteractableException”. <input type=”hidden” /> |
Run-Time Exception:org.openqa.selenium.SessionNotCreatedException
1 2 3 4 5 6 7 8 9 10 11 |
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Facebook { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.facebook.com"); driver.close();//Closing the browser String title = driver.getTitle(); System.out.println("Title of the page::" + title); } } |
Console Output:
1 2 3 |
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Tried to run command without establishing a connection Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:08.638Z' |
Solution:
1 2 |
Once the driver is closed, the session will be closed. Invoking any methods using the object returns an SessionNotCreatedException. |
Run-Time Exception:org.openqa.selenium.NoSuchSessionException
Session ID is null. Using WebDriver after calling quit()
1 2 3 4 5 6 7 8 9 10 11 |
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Facebook { public static void main(String[] args) { WebDriverdriver = newFirefoxDriver(); driver.get("http://www.facebook.com"); driver.quit();//Closing the browser String title = driver.getTitle(); System.out.println("Title of the page::" + title); } } |
Console Output:
1 2 |
Exception in thread "main" org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()? |
Solution:
1 2 |
Once the driver is quit, the session will not be available. Invoking any methods using the object returns an NoSuchSessionException. |
Run-Time Exception:org.openqa.selenium.TimeoutException
Thrown when a command does not complete in enough time.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class IndeedEx { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("https://www.indeed.co.in/"); WebDriverWait wait = new WebDriverWait(driver,30); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("invalid"))); } } |
Console Output:
1 2 3 4 5 |
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.id: invalid (tried for 30 second(s) with 500 milliseconds interval) at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271) |
Solution:
1 |
Make sure the element id passed is valid and able to find with in the Specified time interval. |
Run-Time Exception: org.openqa.selenium.NoSuchWindowException
1 2 3 |
org.openqa.selenium.NoSuchWindowException: Unable to locate window driver.switchTo().window("invalidName"); Switching to invalid window name returns the exception. |
Run-Time Exception: org.openqa.selenium.NoSuchFrameException
1 2 3 |
org.openqa.selenium.NoSuchFrameException: No frame element found by name or id invalidFrame driver.switchTo().frame("invalidFrameName"); Switching to invalid frame name returns the exception. |
Pingback: Rerunning failed tests using IRetryAnalyzer,ITestResult in TESTNG - Total-QA