我想要使用getAttribute
函數獲取屬性值,但是獲取NullPointerException
。可能的原因似乎是在屬性值開始時的雙引號。Selenium WebDriver - 獲取「java.lang.NullPointerException」,同時試圖通過使用「getAttribute」獲取屬性值
任何人都可以提出其他選擇嗎?
硒Java命令:
driver.findElement(By.xpath("//tbody[@role='alert']/tr[@class='error']")).getAttribute("data-suppression-reason");
HTML代碼片段:
<tbody role="alert" aria-live="polite" aria-relevant="all">
<tr class="odd">
<tr class="even">
<tr class="error" data-suppression-reason=""Email" is empty.">
<tr class="error" data-suppression-reason=""Email" is empty.">
<tr class="odd">
</tbody>
堆棧跟蹤:
java.lang.NullPointerException
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.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
請張貼堆棧跟蹤爲好。 – Guy
你確定你的'driver'被初始化了嗎? –
表格在網頁上是否可見?發佈整個表格HTML代碼 –