如何使用带Selenium和Java的XPath提取迭代表中特定行的文本

如何使用带Selenium和Java的XPath提取迭代表中特定行的文本,java,selenium,selenium-webdriver,xpath,webdriverwait,Java,Selenium,Selenium Webdriver,Xpath,Webdriverwait,我想遍历这个表,需要从每一行中获取一个文本,比如td=health1、health2和health3中的文本,同样,我需要状态中的文本。我能做到吗 <table id="TableFormtable" class="datatable" summary=" Server "> <tbody> <tr> <th title="Sort ta

我想遍历这个表,需要从每一行中获取一个文本,比如td=health1、health2和health3中的文本,同样,我需要状态中的文本。我能做到吗

<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']"));
List allRows=utils
.findElements(By.xpath(“//table[@id='genericTableFormtable']/tbody/tr[@id='row偶'或@id='rowOdd']”);
在这一步之后,我如何迭代到每一行并获得相应的值。

要使用Java8创建具有id属性或InnerText的列表,您可以使用以下方法:

  • 打印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()));
    
    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()));
    
  • 打印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..getAttribute("id")).collect(Collectors.toList()));
    
    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()));
    

谢谢,它成功了,您能解释一下我们使用后它在做什么吗。stream@joshua收集了id属性以“运行状况”开头的所需节点后,要获取文本,只需调用
getText()
方法即可。我们创建了一个包含所需文本的列表,并将其打印出来。