I'm trying to click a button using webdriver. Firepath is able to find the button using the xpath I'm giving but selenium is throwing null pointer exception. This page contains two buttons with same id and so, I tried traversing the hierarchy and found one button with a longer xpath.
Here is the HTML:
<section id="overview">
<button id="sign-me-up" class="next col-xs-10 col-xs-offset-1" ng- click="onclick($event, 'owner')">Sign Up Now</button>
My code looks like this: I tried several things here. Three different ways to see whether the element is visible or clickable.
driver.findElement(By.xpath(//section[@id='overview']/button)).click();
driver.findElement(By.xpath(//section[@id='overview']/button[@id='sign-me-up'])).click();
driver.findElement(By.xpath(signupButton)).isDisplayed();
For any of this above, selenium is throwing null pointer exception.
I'm missing something obvious here. Please help.
Full Stack Trace:
FAILED: OnboardAnIndividualProperty
java.lang.NullPointerException
at com.ys.page.SMBSignupLandingPage.assertIndividualOwnerPage(SMBSignupLandingPage.java:31)
at com.ys.tests.SMB.OnboardIndividualApplicationToApprovalState.OnboardAnIndividualProperty(OnboardIndividualApplicationToApprovalState.java:34)
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:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Even this throws the NullPointerException:
driver.findElement(By.xpath(//section[@id='overview']
Does that mean I'm not passing the driver somewhere?
3 Answers 3
According to WebDriver API JavaDoc, if no matching elements are found then the NoSuchElementException
is thrown by findElement()
method, not NullPointerException
. So the root cause of your problem is not a wrong XPath expression.
Instead, your stacktrace and your source code suggests this is a problem with driver
variable not initialized: you try to call findElement()
method on a null
object.
One way to confirm that is to print the driver
value:
System.out.println("driver=" + driver);
just before the exception is thrown, i.e., before line 31 of the SMBSignupLandingPage
class. Or simply run the application in debug mode, put breakpoint in same line and evaluate the value of the variable.
-
1Thank you dzieciou. It is because I missed to inherit the driver from the parent. Driver was null and because of that I was getting NullPointerException but once the driver was inherited, the code works.Midha– Midha2015年05月31日 16:10:49 +00:00Commented May 31, 2015 at 16:10
yo inicialicé la variable en los metodos
(approx. translation: I would initialize the variable with a method like this)
try {
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
}
catch (MalformedURLException e) {
}
-
3While your suggestion to initialize the driver variable is correct, it doesn't add anything to the accepted answer from nearly 4 years ago. I have edited your answer to format the code sample correctly, and given an approximate English translation.Kate Paulk– Kate Paulk2019年01月11日 20:27:50 +00:00Commented Jan 11, 2019 at 20:27
You need to delete the class variable. If you inherit the driver from a base class, but you didn't delete the driver variable from your class. The program tries to use the class's variable instead of the super variable (it has the same name) but the class variable is not initialized, the constructor just calls to the super constructor and you get null pointer exception.
driver.findElement(By.xpath("//section/[@id='overview']"))
signupButton
.NullPointerException
? I'm curious because WebDriver is supposed to throwNoSuchElementException
when it cannot find a matching element. I guess you may getNullPointerException
becausedriver
variable is null.System.out.println("driver=" + driver);
just before exception is thrown, i.e., before line 31 ofSMBSignupLandingPage
class.