0

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?

3
  • Are you looking for the number of names in the list, or a way to store all the names in the list for later?
    – Kate Paulk
    Jun 21, 2019 at 11:42
  • @KatePaulk I am trying to get the names and store as variable ex: Saurav P, Ranjit N. I can use the variables to match in other pages. Thanks!
    – RK007
    Jun 21, 2019 at 12:11
  • I have updated your question with this information - this makes it clearer to others what you are asking
    – Kate Paulk
    Jun 21, 2019 at 13:28

2 Answers 2

1
// 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
3
  • +1 Good answer. One note, it requires JDK 8 or higher (streams).
    – dzieciou
    Jun 21, 2019 at 14:20
  • @João Farias Dint work either.
    – RK007
    Jun 24, 2019 at 6:47
  • What's the error? Jun 24, 2019 at 7:03
0

Below is the answer i got, which is matching exactly what i wanted:

List<WebElement> allText = driver.findElements(By.xpath("//*[@id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"));

        int total = allText.size();
        System.out.println(total);
        for(int i=3;i<=total;i++){
            CaselevelSigningCMs =driver.findElement(By.xpath("//*[@id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"+"["+i+"]"+"/td[2]")).getText();
            System.out.println(CaselevelSigningCMs);
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.