有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Selenium获取列标题下的所有单元格

试图获取特定th(标头)下的所有td(单元格)

HTML:

<table width="800">
    <tbody>
        <tr>
            <th><b>User ID</b></th>
            <th><b>Workforce ID</b></th>
            <th><b>Given Name</b></th>
            <th><b>Surname</b></th>
            <th><b>Division</b></th>
            <th><b>Hire Date</b></th>
            <th><b>Department</b></th>
            <th><b>emplType</b></th>
            <th><b>Associations</b></th>
            <th><b>SAP Roles</b></th>
            <th><b>Community</b></th>
            <th><b>Active</b></th>
        </tr>
        <tr>
            <td class="active"><a href="?username=utest001">utest001</a></td>
            <td class="active">00700001</td>
            <td class="active">User</td>
            <td class="active">Test001</td>
            <td class="active">Coles</td>
            <td class="active">2015-10-27</td>
            <td class="active">CHFITVB</td>
            <td class="active">S</td>
            <td class="active">IAM</td>
            <td class="active"></td>
            <td class="active">Workforce</td>
            <td class="active">Y</td>
        </tr>
        <tr>
            <td class="disabled"><a href="?username=utest002">utest002</a></td>
            <td class="disabled">00700002</td>
            <td class="disabled">Vasia</td>
            <td class="disabled">Smith</td>
            <td class="disabled">Coles</td>
            <td class="disabled">2015-11-16</td>
            <td class="disabled">C0584SV</td>
            <td class="disabled">W</td>
            <td class="disabled"></td>
            <td class="disabled"></td>
            <td class="disabled">Workforce</td>
            <td class="disabled">N</td>
        </tr>
    </tbody>
</table>

方法:

public List<WebElement> getCellsUnderHeader(WebElement table, String headerName) {
    List<WebElement> thList = table.findElements(By.cssSelector("th > b"));
    List<String> headers = new ArrayList<>();
    thList.stream().forEach(th -> headers.add(th.getText()));
    int index = headers.indexOf(headerName);
    List<WebElement> trList = table.findElements(By.tagName("tr"));
    List<WebElement> tdList = new ArrayList<>();
    List<WebElement> columnCells = new ArrayList<>();
    WebElement cell = null;
    for (WebElement tr : trList) {
        tdList = tr.findElements(By.tagName("td"));
        cell = tdList.get(index); // Getting out of bounds here
        columnCells.add(cell);
    }
    return columnCells;
}

实际结果:

java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0

预期结果:

返回头User ID下包含2td(带有a标记)的List<WebElement>


共 (3) 个答案

  1. # 1 楼答案

    trList包含带有标题的<tr>,它没有<td>标记,因此在第一次for迭代中tdList是空的tdList.get(index)抛出IndexOutOfBoundsException

    您可以通过索引进行迭代

    for (int i = 1 ; i < trList.size() ; i++) {
        tdList = trList.get(i).findElements(By.tagName("td"));
        cell = tdList.get(index);
        columnCells.add(cell);
    }
    

    或者把第一件东西运出去

    for (WebElement tr : trList.subList(1, trList.size())) {
        tdList = tr.findElements(By.tagName("td"));
        cell = tdList.get(index);
        columnCells.add(cell);
    }
    
  2. # 2 楼答案

    List<String> userIDs = driver.findElements(By.xpath("//td/a"))
                .stream()
                .map(elem -> elem.getText())
                .collect(Collectors.toList());
    
  3. # 3 楼答案

    问题:

    For the first tr iteration there was no td available, which in result causing the exception

    使用以下命令:

    public List<WebElement> getCellsUnderHeader(WebElement table, String headerName) {
            List<WebElement> thList = table.findElements(By.cssSelector("th > b"));
            List<String> headers = new ArrayList<>();
            thList.stream().forEach(th -> headers.add(th.getText()));
            int index = headers.indexOf(headerName);
            System.out.println("Index : "+index);
            List<WebElement> trList = table.findElements(By.tagName("tr"));
            List<WebElement> tdList = new ArrayList<>();
            List<WebElement> columnCells = new ArrayList<>();
            WebElement cell = null;
            for (WebElement tr : trList) {
                tdList = tr.findElements(By.tagName("td"));
                if (tdList.size()>0){
                    cell = tdList.get(index); // Getting out of bounds here
                    columnCells.add(cell);
                }
            }
            return columnCells;
        }