Store values from table for use in later tests



  • Below is my HTML code. I need to be able to save the names in the table rows (Saurav P, Ranjit M) to use in later tests.

    <table cellspacing="0" cellpadding="0" summary="Case managers who sign forms for this Case" width="100%" class="TableStyle">
        <tbody>
            <tr>
                <td colspan="25" id="PARAHDR" style="padding-left:0px">
                    <table style="border:none 0px" cellpadding="0" cellspacing="0">
                        <tbody>
                            <tr>
                                <td id="PARAHDR" style="width:3px">
                                    <!--<IMG src='../images/icons/dash_lt_end_7.gif'>-->
                                </td>
                                <td id="PARAHDR">Case managers who sign forms for this Case</td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
            <tr>
                <th class="LISTHDR" align="LEFT" width="2%" valign="TOP">&nbsp;</th>
                <th class="LISTHDR" align="LEFT" width="98%" valign="TOP">Name </th>
            </tr>
            <tr class="TBLROW1">
                <td class="TDOCLIST">&nbsp;1&nbsp;</td>
                <td class="TDOCLIST">&nbsp;Saurav P&nbsp;</td>
            </tr>
            <tr class="TBLROW2">
                <td class="TDOCLIST">&nbsp;2&nbsp;</td>
                <td class="TDOCLIST">&nbsp;Ranjit N&nbsp;</td>
            </tr>
        </tbody>
    </table>
    

    How do I use Selenium to find the names and save them into a global variable that I can use later?



  • // Find all rows with a class with TBLROW as sub-string
    List<WebElement> rowsWithName = driver.findElements(By.cssSelector("tr[class*=\"TBLROW\"]"));  
    List<String> names = rowsWithName.stream().map(row -> // For each row
                            row.findElements(By.className("TDOCLIST")) // Find all TDOCLIST cells
                               .get(1) // Get the second one
                               .getText()) // And extract its text
                               .collect(Collectors.toList()); // Afterwards, gather all texts in a list
    


Suggested Topics