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

时间:2019-10-17 10:12:58

标签: java selenium selenium-webdriver

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

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:索引0超出长度0的范围

预期结果:

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

3 个答案:

答案 0 :(得分:2)

问题:

  

对于第一个 tr 迭代,没有可用的 td ,   结果导致异常

使用以下内容:

conda install -c conda-forge opencv

答案 1 :(得分:2)

.exec( exec(http("request_7") .get(uri1 + "/negotiate?clientProtocol=1.5&Authorization=Bearer%20${authToken}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D") .headers(headers_7)), exec(http("request_8") .get("/api/notifications") .headers(headers_2)), exec(http("request_9") .get(uri1 + "/start?transport=serverSentEvents&clientProtocol=1.5&Authorization=Bearer%20${authToken}&connectionToken=${MyConnectionToken}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D") .headers(headers_7))) 包含带有标题的trList,它没有<tr>标签,因此在第一个<td>迭代中,for为空,{{ 1}}抛出tdList

您可以按索引进行迭代

tdList.get(index)

或运送第一件物品

IndexOutOfBoundsException

答案 2 :(得分:0)

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