我有以下的HTML代碼,我需要檢查,如果文本表格單元格中存在:硒XPath查找div標籤內的表格單元格
<div class="background-ljus" id="AutoText">
<table class="topAlignedCellContent">
<tbody>
<tr>
<td>X1</td>
</tr>
<tr>
<td>X2</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Y1</td>
<td>Y2</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Z1</td>
<td>Z2</td>
</tr>
</tbody>
</table>
</div>
我已經解決了這樣的:
By locator = getLocator(CommonConst.XPATH, "*//div[@" + type + "='" + anyName + "']");
fluentWait(locator);
WebElement div = getDriver().findElement(locator);
List<WebElement> cells = div.findElements(By.tagName("td"));
for (WebElement cell : cells) {
if (cell.getText().contains(cellText)) {
foundit = true;
}
}
但我覺得它有點慢,因爲我需要這樣做幾次。 我試圖用XPath做到這一點,但沒有運氣。
"*//div[@id='AutoText']//td[contains[Text(), 'celltext']]"
"*//div[@id='AutoText']//table//tbody//tr[td//Text()[contains[., 'celltext']]"
任何人都有關於爲什麼我的XPath不工作的建議嗎?
退房我添加的說明我的回答,它指出了一個重要的警告。 – Tomalak