如何使用Selenium和Java通过XPath迭代表格中特定行并提取文本。

huangapple go评论94阅读模式
英文:

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

&lt;table id=&quot;TableFormtable&quot; class=&quot;datatable&quot; summary=&quot; Server &quot;&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;th title=&quot;Sort table by Name&quot; scope=&quot;col&quot;&gt;&lt;/th&gt;
            &lt;th title=&quot;Sort table by clusterName&quot; scope=&quot;col&quot;&gt;&lt;/th&gt;
            &lt;th title=&quot;Sort table by Machine&quot; scope=&quot;col&quot;&gt;&lt;/th&gt;
            &lt;th title=&quot;Sort table by State&quot; scope=&quot;col&quot;&gt;&lt;/th&gt;
            &lt;th title=&quot;Sort table by Health&quot; scope=&quot;col&quot;&gt;&lt;/th&gt;
            &lt;th title=&quot;Sort table by port&quot; scope=&quot;col&quot;&gt;&lt;/th&gt;
        &lt;/tr&gt;
        &lt;tr class=&quot;rowEven&quot;&gt;
            &lt;td id=&quot;name1&quot; scope=&quot;row&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;clusterName1&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;machineName1&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;state1&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;health1&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;port1&quot;&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class=&quot;rowOdd&quot;&gt;
            &lt;td id=&quot;name2&quot; scope=&quot;row&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;clusterName2&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;machineName2&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;state2&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;health2&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;port2&quot;&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class=&quot;rowEven&quot;&gt;
            &lt;td id=&quot;name3&quot; scope=&quot;row&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;clusterName3&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;machineName3&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;state3&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;health3&quot;&gt;&lt;/td&gt;
            &lt;td id=&quot;port3&quot;&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

I am using

 List&lt;WebElement&gt; allRows = utils
				.findElements(By.xpath(&quot;//table[@id=&#39;genericTableFormtable&#39;]/tbody/tr[@id=&#39;rowEven&#39; or @id=&#39;rowOdd&#39;]&quot;));

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(&quot;//table[@id=&#39;TableFormtable&#39;]/tbody//tr[@class=&#39;rowEven&#39; or @class=&#39;rowOdd&#39;]//td[starts-with(@id, &#39;health&#39;)]&quot;))).stream().map(element-&gt;element.getAttribute(&quot;id&quot;)).collect(Collectors.toList()));
    
  • Printing the innerText attributes:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(&quot;//table[@id=&#39;TableFormtable&#39;]/tbody//tr[@class=&#39;rowEven&#39; or @class=&#39;rowOdd&#39;]//td[starts-with(@id, &#39;health&#39;)]&quot;))).stream().map(element-&gt;element.getText()).collect(Collectors.toList()));
    

huangapple
  • 本文由 发表于 2020年6月29日 18:27:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/62636135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定