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.
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:
Post a Comment