如何使用带有 Selenium 和 Java 的 XPath 在 table 中提取迭代特定行的文本
How to extract the text iterating specific rows within a table using XPath with Selenium and Java
我想遍历此 table 并需要从每一行中获取文本,例如 td= health1、health 2 和 health 3 中的文本,同样,我需要 state.Hoe 中的文本我可以这样做吗
<table id="TableFormtable" class="datatable" summary=" Server ">
<tbody>
<tr>
<th title="Sort table by Name" scope="col"></th>
<th title="Sort table by clusterName" scope="col"></th>
<th title="Sort table by Machine" scope="col"></th>
<th title="Sort table by State" scope="col"></th>
<th title="Sort table by Health" scope="col"></th>
<th title="Sort table by port" scope="col"></th>
</tr>
<tr class="rowEven">
<td id="name1" scope="row"></td>
<td id="clusterName1"></td>
<td id="machineName1"></td>
<td id="state1"></td>
<td id="health1"></td>
<td id="port1"></td>
</tr>
<tr class="rowOdd">
<td id="name2" scope="row"></td>
<td id="clusterName2"></td>
<td id="machineName2"></td>
<td id="state2"></td>
<td id="health2"></td>
<td id="port2"></td>
</tr>
<tr class="rowEven">
<td id="name3" scope="row"></td>
<td id="clusterName3"></td>
<td id="machineName3"></td>
<td id="state3"></td>
<td id="health3"></td>
<td id="port3"></td>
</tr>
</tbody>
</table>
我正在使用
List<WebElement> allRows = utils
.findElements(By.xpath("//table[@id='genericTableFormtable']/tbody/tr[@id='rowEven' or @id='rowOdd']"));
完成此步骤后,我如何遍历每一行并获取相应的值。
使用 id 属性或 innerTexts 使用 [=37 创建一个 List =]的stream()
and map()
you can use the following xpath based :
正在打印 id 属性:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//table[@id='TableFormtable']/tbody//tr[@class='rowEven' or @class='rowOdd']//td[starts-with(@id, 'health')]"))).stream().map(element->element.getAttribute("id")).collect(Collectors.toList()));
正在打印 innerText 属性:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//table[@id='TableFormtable']/tbody//tr[@class='rowEven' or @class='rowOdd']//td[starts-with(@id, 'health')]"))).stream().map(element->element.getText()).collect(Collectors.toList()));
我想遍历此 table 并需要从每一行中获取文本,例如 td= health1、health 2 和 health 3 中的文本,同样,我需要 state.Hoe 中的文本我可以这样做吗
<table id="TableFormtable" class="datatable" summary=" Server ">
<tbody>
<tr>
<th title="Sort table by Name" scope="col"></th>
<th title="Sort table by clusterName" scope="col"></th>
<th title="Sort table by Machine" scope="col"></th>
<th title="Sort table by State" scope="col"></th>
<th title="Sort table by Health" scope="col"></th>
<th title="Sort table by port" scope="col"></th>
</tr>
<tr class="rowEven">
<td id="name1" scope="row"></td>
<td id="clusterName1"></td>
<td id="machineName1"></td>
<td id="state1"></td>
<td id="health1"></td>
<td id="port1"></td>
</tr>
<tr class="rowOdd">
<td id="name2" scope="row"></td>
<td id="clusterName2"></td>
<td id="machineName2"></td>
<td id="state2"></td>
<td id="health2"></td>
<td id="port2"></td>
</tr>
<tr class="rowEven">
<td id="name3" scope="row"></td>
<td id="clusterName3"></td>
<td id="machineName3"></td>
<td id="state3"></td>
<td id="health3"></td>
<td id="port3"></td>
</tr>
</tbody>
</table>
我正在使用
List<WebElement> allRows = utils
.findElements(By.xpath("//table[@id='genericTableFormtable']/tbody/tr[@id='rowEven' or @id='rowOdd']"));
完成此步骤后,我如何遍历每一行并获取相应的值。
使用 id 属性或 innerTexts 使用 [=37 创建一个 List =]的stream()
and map()
you can use the following xpath based
正在打印 id 属性:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//table[@id='TableFormtable']/tbody//tr[@class='rowEven' or @class='rowOdd']//td[starts-with(@id, 'health')]"))).stream().map(element->element.getAttribute("id")).collect(Collectors.toList()));
正在打印 innerText 属性:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//table[@id='TableFormtable']/tbody//tr[@class='rowEven' or @class='rowOdd']//td[starts-with(@id, 'health')]"))).stream().map(element->element.getText()).collect(Collectors.toList()));