Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Monday, February 11, 2019

How to return all the values of a column in a web table. Selenium and xpath

Fellow automation developers, Many times during web automation we come across scenarios where we want to retrieve all the values of a column based upon the column name of a dynamic web table.

Consider in the below image, you want to get all company names. 


Or you want to get all the Contact names. 

Below are the XPath and selenium-java method to find and return all name of the company column. 



/**
  * @author diehardtechy
  * @param columnName
  *            : Column name whose all
  *            <td>values needs to be returned.
  * @return List<String> 
  */
 public List<String> getColumnValues(String columnName, String textToSearch) {

  String xpath = "//table[@id='customers']/tbody/tr/td[count(//table/tbody/tr/th[.=\"" + columnName
    + "\"]/preceding-sibling::th)+1]";

  List<WebElement> elements = d.findElements(By.xpath(xpath));
  return elements.stream().map(t->t.getText()).collect(Collectors.toList());
  
 }


No comments: