1

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3263913
  • 13
  • 2
  • 5
  • Could you be more clear? If everything is fixed and not dynamic whats the problem? what code you have tried? – Purus Jul 04 '14 at 04:03

2 Answers2

2

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();
Santoshsarma
  • 5,627
  • 1
  • 24
  • 39
  • Santosh - i Want to retrieve value2. I dont know what value it is. Can I do that with following-sibling? As of now, I tried one solution mentioned below. It worked for me. However, If I can find shorter solution then I use shorter one. – user3263913 Jul 08 '14 at 18:12
  • We can achieve that. I've modified my answer. Other solution is something like iterate the entire table until you find td tag with value companyName.My solution is directly finding td which has required value. – Santoshsarma Jul 09 '14 at 04:00
0

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;
        }
    }
user3263913
  • 13
  • 2
  • 5