-1

In my selenium script , I want to get the table data from below table, and compare all values under column 'phone' and check if all the values are only numeric. I tried to use the following code, if is not printing the text of each column, instead it is displaying number of rows.

WebElement table = driver.findElement(By.xpath(“//table[contains(@class, 'table table-style')]”));
        List<WebElement> rowsList = table.findElements(By.tagName("tr"));

        List<WebElement> columnsList = null;

       for (WebElement row : rowsList) {
               columnsList = row.findElements(By.tagName("td"));

                for (WebElement column : columnsList) {
                       System.out.println("column text" + column.getText()+ ", "); // here is is just printing number of rows, like 1, 2
               }

        }

<table class="table table-style" >
    
    <thead>
    <tr>
    <th class="text-center">
            Id
    </th>
    <th class="text-center">username</th>
    <th class="text-center">email</th>
    <th class="text-center">phone</th>
    <th class="text-center">address1</th>
    <th class="text-center">address2</th>
    <th class="text-center">City</th>
    </tr>
    </thead>
    
    <tbody>
    <tr>
    
    <td class="text-center">Here is user name</td>
    <td class="text-center">email@email.com</td>
    <td class="text-center">123456789</td>
    <td class="text-center">address 1</td>
    <td class="text-center">address 2</td>
    <td class="text-center">city name</td>
    </tr>
        <tr>
    
    <td class="text-center">Here is user name</td>
    <td class="text-center">email@email.com</td>
    <td class="text-center">99999999</td>
    <td class="text-center">address 1</td>
    <td class="text-center">address 2</td>
    <td class="text-center">city name</td>
    </tr>
    
     <tr>
    
    <td class="text-center">Here is user name</td>
    <td class="text-center">email@email.com</td>
    <td class="text-center">abcdef12</td>
    <td class="text-center">address 1</td>
    <td class="text-center">address 2</td>
    <td class="text-center">city name</td>
    </tr>
    
</table>
user1015388
  • 1,283
  • 4
  • 25
  • 45

1 Answers1

0

Get the cell value by the column name

There is no any additional attributes in your case to detect that some cell belongs to some column.

I suggest to detect the column number by text using XPATH count method, and use it for getting all cells by column.

For the phone column:

List<WebElement> phoneNumbersList = table.findElements(By.xpath("//td[count(//th[text()='phone']/preceding-sibling::th)]"));

// the same can be dome for "email" and other columns.

Check if all the values are only numeric

Look at this: Java String - See if a string contains only numbers and not letters


I tried to use the following code, if is not printing the text of each column, instead it is displaying number of rows.

I can confirm that this code:

@Test
public void printTable() {
    WebDriver driver = new ChromeDriver(new ChromeOptions());
    driver.manage().window().maximize();
    //local html file
    driver.get("file:///D:/projects/print-table/src/test/resources/test.html");

    WebElement table = driver.findElement(By.xpath("//table[contains(@class, 'table table-style')]"));
    List<WebElement> rowsList = table.findElements(By.tagName("tr"));

    List<WebElement> columnsList = null;

    for (WebElement row : rowsList) {
        columnsList = row.findElements(By.tagName("td"));

        for (WebElement column : columnsList) {
            System.out.println("column text" + column.getText()+ ", "); // here is is just printing number of rows, like 1, 2
        }

    }
    driver.quit();
}

Prints:

column textHere is user name, 
column textemail@email.com, 
column text123456789, 
column textaddress 1, 
column textaddress 2, 
column textcity name, 
column textHere is user name, 
column textemail@email.com, 
column text99999999, 
column textaddress 1, 
column textaddress 2, 
column textcity name, 
column textHere is user name, 
column textemail@email.com, 
column textabcdef12, 
column textaddress 1, 
column textaddress 2, 
column textcity name, 

So, your provided snippet works as expected.

Max Daroshchanka
  • 2,698
  • 2
  • 10
  • 14
  • 1
    your approach works great! I noticed that PHONE column TH is surrounded by some little extra HTML text shown below, How can I use preceding-sibling here, I tried this which is nor working. By.xpath("//td[count(//a[text()='Phone']/preceding-sibling::a)]" Phone – user1015388 Feb 04 '22 at 15:50
  • 1
    @user1015388 for this new page source try this `//td[count(//th[contains(., 'Phone')]/preceding-sibling::th)]`. `contains` here because `Phone` surrounded with spaces. And `.` means that text might be in some child element. – Max Daroshchanka Feb 05 '22 at 09:01