当我执行以下代码时:
driver.findElement(By.xpath("//h5[text()='Users and Groups Management']")).click();
我得到以下错误
Element info: {Using=xpath, value=//h5[text()='Users and Groups Management']}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at Basic.main(Basic.java:23)
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
该页面包含以下行,我也尝试等待适当的时间来执行,也新的WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h5[text()='Users and Groups Management']")).click();
没有工作!
<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>
<h5 _ngcontent-ncj-c134="" class="text-center font-weight-bold m-0"> Users and Groups Management </h5>
请分享你的想法,我如何才能继续这一点。谢谢!
1条答案
按热度按时间qmb5sa221#
我建议将xpath改为通用的,从长远来看肯定有助于可维护性。当我在与特定元素(包括H5)交互时遇到麻烦时,这是我要走的路。