If you want to select column element by header text in rows and use it in Selenium, here some examples:
Example of HTML:
<table> <thead> <tr> <th>LION</th> <th>TIGER</th> <th>CHEETAH</th> <th>GIRAFFE</th> <th>BEAR</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> </tr> </tbody> </table>
XPATH for Selenium usage:
//table/tbody/tr/td[count(//table//th[.="GIRAFFE"]/preceding::th)+1]
Selenium Java usage example:
String animalName = "GIRAFFE"; WebElement element = driver.findElement(By.xpath("//table/tbody/tr/td[count(//table//th[.='" + animalName + "']/preceding::th)+1]")); WebElement animalElementFromRow1 = driver.findElement(By.xpath("//table/tbody/tr[1]/td[count(//table//th[.='" + animalName + "']/preceding::th)+1]")); WebElement animalElementFromRow2 = driver.findElement(By.xpath("//table/tbody/tr[2]/td[count(//table//th[.='" + animalName + "']/preceding::th)+1]"));
//th[.='GIRAFFE']to select the element?