1

My table looks like below: clm1 clm2 clm3

  1. 1 b hi
  2. 2 c hello
  3. 3 d hi

Now the requirement is I have to find all the 'hi' and click on the other cell of same row. For e.g. in 1st occurance I have to click on 1 and the again on 3.

I am able to find 'hi' with below code but how to find the corresponding cell on the same row.

List rows = driver.findElements(By.xpath("//span[text()='hi']"));

<table>
  <tr class="abc">
    <td class="efg">
      <a id="asg">1</a>
    </td>
  </tr>
  <tr class="abc">
    <td class="efg">
      <span>1</span>
    </td>
  </tr>
  <tr class="abc">
    <td class="efg">
      <span>1</span>
    </td>
  </tr>
</table>

Please ignore the typo as I am typing from mobile. Any help will be highly appreciated.

mj3c
  • 1,447
  • 15
  • 18
Shibankar
  • 806
  • 3
  • 16
  • 40
  • Could you post the link to the site or even post the html code. Currently it is hard to imagine how it actually looks. – Madis Kangro Jul 14 '17 at 11:35
  • Possible duplicate of [Selenium webdriver get text of adjacent column in same row by matching text from other column](https://stackoverflow.com/questions/27710460/selenium-webdriver-get-text-of-adjacent-column-in-same-row-by-matching-text-from) – Florian Albrecht Jul 14 '17 at 11:46
  • Can you rephrase the verbatim of the Question as per the HTML DOM you provided? I can see only one alphabet `1`. Still not sure how you find `hi` and `hello` both. Thanks – undetected Selenium Jul 14 '17 at 15:18

3 Answers3

2

By.xpath("//td[./span[text()='hi']]/../td[1]") would return the first column of that matching row.

Florian Albrecht
  • 2,266
  • 1
  • 20
  • 25
1

Here is something simple in C#. I hope that you will be able to convert it to java.

var tableRows = driver.FindElements(By.TagName("tr"));
foreach(var tableRow in tableRows)
{
   var td = tableRow.FindElements(By.TagName("td"));
   if(td[2].Text.Contains("hi"))
   {
      td[0].FindElement(By.TagName("a")).Click();
   }
}
Rostech
  • 322
  • 1
  • 11
0

If i understand you should use xpath:

.//yourow/span[not(text()='Hi')][..//span[text()='Hi']]

You get all "brothers" or "sisters" in html tree which do not have text Hi but are in the same row as cell contain text "Hi"

qchar90
  • 404
  • 1
  • 5
  • 11