Selenium中的sun.reflect.NativeConstructorAccessImpl.newInstance0(Native Method)错误

yfwxisqw  于 2023-05-22  发布在  其他
关注(0)|答案(1)|浏览(232)

当我执行以下代码时:

  1. driver.findElement(By.xpath("//h5[text()='Users and Groups Management']")).click();

我得到以下错误

  1. Element info: {Using=xpath, value=//h5[text()='Users and Groups Management']}
  2. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  3. at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  4. at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  5. at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  6. at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
  7. at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
  8. at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
  9. at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
  10. at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
  11. at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
  12. at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
  13. at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
  14. at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
  15. at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
  16. at Basic.main(Basic.java:23)
  17. Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true

该页面包含以下行,我也尝试等待适当的时间来执行,也新的WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h5[text()='Users and Groups Management']")).click();没有工作!

  1. <div _ngcontent-ncj-c134="" class="card-header flex-centrally-aligned border-0"><h5 _ngcontent-ncj-c134="" class="text-center font-weight-bold m-0"> Users and Groups Management </h5></div>
  2. <h5 _ngcontent-ncj-c134="" class="text-center font-weight-bold m-0"> Users and Groups Management </h5>

请分享你的想法,我如何才能继续这一点。谢谢!

qmb5sa22

qmb5sa221#

我建议将xpath改为通用的,从长远来看肯定有助于可维护性。当我在与特定元素(包括H5)交互时遇到麻烦时,这是我要走的路。

  1. driver.findElement(By.xpath("//h5[text()='Users and Groups Management']")).click();
  2. driver.findElement(By.xpath("//*/*[contains(@text(), 'Users and Groups Management')]")).click();

相关问题