2

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?

dzieciou
10.5k9 gold badges49 silver badges102 bronze badges
asked May 30, 2015 at 15:43
5
  • 1
    Let's go with a process of elimination. Please edit your question to specify whether this throws a NullPointException as well: driver.findElement(By.xpath("//section/[@id='overview']")) Commented May 30, 2015 at 16:12
  • Also, you did know specify the definition of signupButton. Commented May 30, 2015 at 16:13
  • Can you also provide full stacktrace with NullPointerException? I'm curious because WebDriver is supposed to throw NoSuchElementException when it cannot find a matching element. I guess you may get NullPointerException because driver variable is null. Commented May 30, 2015 at 19:45
  • I have added the full stack trace in the original post. Commented May 30, 2015 at 20:22
  • 1
    Try to print driver value: System.out.println("driver=" + driver); just before exception is thrown, i.e., before line 31 of SMBSignupLandingPage class. Commented May 30, 2015 at 20:38

3 Answers 3

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.

answered May 31, 2015 at 14:55
1
  • 1
    Thank 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. Commented May 31, 2015 at 16:10
0

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) {
 }
Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
answered Jan 11, 2019 at 19:42
1
  • 3
    While 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. Commented Jan 11, 2019 at 20:27
0

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.

Prome
1,01511 silver badges25 bronze badges
answered Jul 8, 2020 at 7:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.