Java 通过xpath w.r.t查找到其他元素的元素
我试图通过Xpath w.r.t其他元素的Xpath找到一个元素 在以下HTML结构中:Java 通过xpath w.r.t查找到其他元素的元素,java,selenium,xpath,Java,Selenium,Xpath,我试图通过Xpath w.r.t其他元素的Xpath找到一个元素 在以下HTML结构中: <div id='frmKonyLib_pxwgmctProj'> <ul> <li> <ul> <li> <table> <tbody>
<div id='frmKonyLib_pxwgmctProj'>
<ul>
<li>
<ul>
<li>
<table>
<tbody>
<tr>
<td>
<img> ------>node 1
</td>
<td>
</td>
</tr>
</tbody>
</table>
<ul show = "true"> ---> element is visible
<li>
<table>
<tbody>
<tr>
<td>
<img> ------>node 2
</td>
<td>
<a>text node1</a>
</td>
</tr>
</tbody>
</table>
<ul show = "false">
<li>
<table>
<tbody>
<tr>
<td>
<img> ------>node 3
</td>
<td>
<a>text node2</a>
</td>
</tr>
</tbody>
</table>
<ul>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>
</li>
</ul>
</li>
<li>
</li>
</ul>
</div
在上面的代码中找到formElement,但会为widgetElement引发异常。
请帮助我找出错误和逻辑的代码建议
下面是引发的异常:
'Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:171)
at org.openqa.selenium.remote.RemoteWebElement.findElementByXPath(RemoteWebElement.java:244)
at org.openqa.selenium.By$ByXPath.findElement(By.java:344)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:167)
at com.kony.vizAutomation.px.utils.PXUtils.selectWidgetByID(PXUtils.java:195)
at com.kony.seleniumexample.SelectWidgetInPX.main(SelectWidgetInPX.java:16)
“使用//启动xpath将搜索文档根目录中的所有子体。如果只想搜索给定元素的后代,请以点开始:.//。类似地,以/开头的xpath将从文档的根开始搜索。要从给定元素进行搜索,只需删除/,因此
ancestor::table[1]/following-sibling::ul//a[text()='text node2']
而不是
/ancestor::table[1]/following-sibling::ul//a[text()='text node2']
/ancestor::table[1]/following-sibling::ul//a[text()='text node2']