![](/img/trans.png)
[英]Wildfly 8 : Not able to deploy Jolokia running at different port w.r.t other applications under same IP
[英]Find an element by xpath w.r.t to other element
我試圖通過Xpath找到一個元素,而其他元素的xpath卻沒有。
在下面的HTML結構中:
<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
我編寫的用於查找node1,textnode1和textnode2的代碼是:
WebElement templateElement = driver.findElement(By.xpath("//*[@id='frmKonyLib_pxwgmctProj']/ul/li[1]/ul/li[1]/table/tbody/tr/td[1]/img"));
WebElement formElement = templateElement.findElement(By.xpath("//a[text()='text node1']"));
WebElement widgetElement = formElement.findElement(By.xpath("/ancestor::table[1]/following-sibling::ul//a[text()='text node2']"));
在上面的代碼中,找到了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']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.