Reputation: 13
My question is similar to this How to find specific lines in a table using Selenium? except one thing that table rows can be in random order and I want to find specific value from specific row that that has fix column1 value. i.e. find budget if company="abc". company "abc" can appear in any row in the table.
Example:
column1: column2: column3
company1: value1: value2:
company2: value1: value2
Over here I want to find value2 for company2. company2 can appear anywhere in the table.
Upvotes: 1
Views: 6238
Reputation: 5667
You can try with xpath locator.
for below table structure
<table>
<tr> <td> company2</td> <td> value1</td> <td> value2</td> </tr>
</table>
Xpath
"//td[text()='company2']/following-sibling::td[text()='value2']"
By.xpath("//td[text()='company2']/following-sibling::td[text()='value2']");
EDIT I
for getting text from td you can use the same with some index
driver.findElement(By.xpath("//td[text()='company2']/following-sibling::td[2]")).getText();
Upvotes: 2
Reputation: 13
Thanks SantoshSharma. I will try this. It looks short and simple.
Purus & Santosh - As of now, I tried below code and it worked for me.
java.util.List tableRows = baseTable.findElements(By.tagName("tr"));
for (WebElement row : tableRows) {
String companyNameXPath = "td[2]";
WebElement companyName = row.findElement(By.xpath(companyNameXPath));
if (companyName.getText().equals(company)) {
valueString = row.findElement(By.xpath("td[4]")).getText();
break;
}
}
Upvotes: 0