英文:
How to extract the text iterating specific rows within a table using XPath with Selenium and Java
问题
我想要遍历这个表格,并且需要从每一行中获取一个文本,例如在td元素内的health1、health2和health3,同样地,我需要获取state元素内的文本。我应该如何做到这一点?
List<WebElement> allRows = utils.findElements(By.xpath("//table[@id='genericTableFormtable']/tbody/tr[@class='rowEven' or @class='rowOdd']"));
for (WebElement row : allRows) {
WebElement healthCell = row.findElement(By.id("health" + (allRows.indexOf(row) + 1)));
WebElement stateCell = row.findElement(By.id("state" + (allRows.indexOf(row) + 1)));
String healthText = healthCell.getText();
String stateText = stateCell.getText();
// 在这里处理healthText和stateText的值
}
英文:
I want to iterate through this table and need to get a text from each row like text inside td= health1, health 2 and health 3, similarly, I need text inside the state.Hoe can I do that
<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>
I am using
List<WebElement> allRows = utils
.findElements(By.xpath("//table[@id='genericTableFormtable']/tbody/tr[@id='rowEven' or @id='rowOdd']"));
after this step how can I iterate to each row and get the respective values.
答案1
得分: 1
// 使用Java8的stream()和map()方法来创建带有id属性或innerText的_List_
// 打印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()));
英文:
To create a List with the id attributes or the innerTexts using Java8's stream()
and map()
you can use the following [tag:xpath] based Locator Strategies:
-
Printing the id attributes:
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()));
-
Printing the innerText attributes:
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()));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论