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?

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

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