<span id="UniversalRepositoryExplorer_treeNode7_name" style="white-space:nowrap;"> == $0
<img id="UniversalRepositoryExplorer_treeNodeIcon_7" src="../../images/server_running.gif"
style="width:16px;height:16px;" alt border="0"> == $0
" Running " == $0
標簽跨度有內部標簽 img 和下面的文字 Running 沒有任何標簽名稱
我嘗試了以下無效的 x-path:
//img[@id='UniversalRepositoryExplorer_treeNodeIcon_7']
有人可以向我建議如何通過 x-path 運行嗎?
uj5u.com熱心網友回復:
基本上,您正在嘗試獲取文本節點。
路徑: '//span/text()[last()]'
例子:
document.evaluate("//span/text()[last()]", window.document, null, XPathResult.ANY_TYPE, null).iterateNext()
// output: " Running "
uj5u.com熱心網友回復:
看看你上面提到的元素,如果我試圖評估一個黃瓜步驟,我會為 selenium-java 寫這個:
@And("^I can view the \"([^\"]*)\" image$")
public void iCanViewTheImage(String text) throws Throwable {
String imgXpath = "//span[contains(text(),'" text "') and contains(@id, 'UniversalRepositoryExplorer_treeNode7_name')]//img[@id='UniversalRepositoryExplorer_treeNodeIcon_7']"
driver.findElement(By.xpath(imgXpath));
assertTrue(driver.findElement(By.xpath(imgXpath)).getText().contains(text));
}
在你的黃瓜步驟中,正則運算式“([^”]*)”將是“運行”:我可以查看“運行”影像
基于上述文本的 xpath 將是:
//span[contains(text(),'Running') and contains(@id, 'UniversalRepositoryExplorer_treeNode7_name')]//img[@id='UniversalRepositoryExplorer_treeNodeIcon_7']
或者干脆
//span[contains(text(),'Running') and contains(@id, 'UniversalRepositoryExplorer_treeNode7_name')]
uj5u.com熱心網友回復:
您的定位器的問題在于 IMG 標簽是自動關閉的(沒有</img>標簽),因此它不能包含文本。父 SPAN 可能是包含“運行”文本的元素。
您還沒有為所討論的部分發布有效的 HTML,所以下面的 HTML 是我猜測的一般結構的樣子。
<span id="UniversalRepositoryExplorer_treeNode7_name" style="white-space:nowrap;">
<img id="UniversalRepositoryExplorer_treeNodeIcon_7" src="../../images/server_running.gif" style="width:16px;height:16px;" alt border="0">
" Running "
</span>
鑒于該 HTML,代碼看起來像
driver.findElement(By.id("UniversalRepositoryExplorer_treeNode7_name")).getText();
當您獲取 SPAN 然后使用 時.getText(),它將獲得該 SPAN 中包含的所有文本。這當然是一個猜測,因為您還沒有發布該跨度下的所有 HTML。這可能比您想要的更多,但這是我們對不完整問題所能做的最好的事情。如果您添加更多詳細資訊,我們可以調整我們的答案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/339954.html
