30
Jul

Identify the root cause of exception in Selenium

Consider following code snippet for “NoSuchElementException” condition in Selenium

WebDriver driver;
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(“http://www.google.co.in”);
WebDriverWait wait = new WebDriverWait(driver, 20);
By addItem = By.name(“q”);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));

element.sendKeys(“Hello google”);

driver.findElement(By.xpath(“//input[@name=’btnK1′]”)).click();

example of invalid element

output:
*** Element info: {Using=xpath, value=//input[@name=’btnK1′]}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:490)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:345)
at SeleniumPractice.TestNGExample.SysOut(TestNGExample.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {“method”:”xpath”,”selector”:”//input[@name=’btnK1′]”}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

While working with exception message, One can reduce the Stack trace and log relevant piece of information as follow:
try{
WebDriver driver;
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(“http://www.google.co.in”);
WebDriverWait wait = new WebDriverWait(driver, 20);
By addItem = By.name(“q”);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));

element.sendKeys(“Hello google”);

driver.findElement(By.xpath(“//input[@name=’btnK1′]”)).click();
} catch (Exception e) {
while (e.getCause() != null)
e = (Exception) e.getCause();
System.out.println(“Root cause is ” + e.getMessage());
}

Output:
Root cause is Unable to locate element: {“method”:”xpath”,”selector”:”//input[@name=’btnK1′]”}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html