2

<tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>

[https://www.w3schools.com/html/html_tables.asp]

you can find the sample web table in the link. I want contact value with the company and country.

I have tried something like this

//td[text()='Laughing Bacchus Winecellars']//following-sibling::td and //td[text()='Canada']//preceding-sibling::td
JaSON
  • 4,843
  • 2
  • 8
  • 15
Prabhanjan
  • 25
  • 5

4 Answers4

1

To extract the middle column value i.e. Maria Anders you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • Using the Company text Alfreds Futterkiste:

    • xpath:

      //table[@id='customers']//tr//td[text()='Alfreds Futterkiste']//following::td[1]
      
    • Line of code:

      System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@id='customers']//tr//td[text()='Alfreds Futterkiste']//following::td[1]"))).getText());
      
  • Using the Country text Germany:

    • xpath:

      //table[@id='customers']//tr//td[text()='Germany']//preceding::td[1]
      
    • Line of code:

      System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@id='customers']//tr//td[text()='Germany']//preceding::td[1]"))).getText());
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can loop trough the <tr> and use //tr/td[2] to select the second <td>.

http://xpather.com/aHyybZni

HedgeHog
  • 22,146
  • 4
  • 14
  • 36
0

Try this one to select required td node:

//td[preceding-sibling::td[.="Alfreds Futterkiste"] and following-sibling::td[.="Germany"]]
JaSON
  • 4,843
  • 2
  • 8
  • 15
0

Try following xpath:

//td[preceding-sibling::td[text()='Laughing Bacchus Winecellars'] and following-sibling::td[text()='Canada']]
frianH
  • 7,295
  • 6
  • 20
  • 45