0
<td>
    <span class="td-title">city</span>
    <span>city</span>
    " Japan"
</td>

How to locate element on text by using XPath? I tried to use /td/text() but it showed "td/text()" is: [object Text]. It should be an element. If I use /td it will return city city Japan , but I want the only output is Japan.

I cannot use contain() or /td[text() = 'Japan'] to specific city "Japan", because I also want to find multiple different cities. If I use this functions, I'll have to manually change city's name every time.

mavicll
  • 31
  • 1
  • 5
  • What do you mean by "it's not working"? What is returned and what do you want to return, or what are you looking for? Describe exactly what you want to achieve. – Mads Hansen Feb 20 '22 at 01:28
  • Is the issue that `/td/text()` is including extra white space (carriage returns and spaces)? – Mads Hansen Feb 20 '22 at 01:29
  • @MadsHansen sorry my question was not clear, I just updated it. I cannot specific "Japan", because I also want to output other cities after that, I don't want to manually change it. – mavicll Feb 20 '22 at 11:57

1 Answers1

0

If you want to select td elements that have a text() node child that is not just whitespace, you can use

/td[text()[normalize-space()]]

But if you are selecting that element and ask for its computed string value, it will concat all of the text() node descendants.

You would want to select only the immediate child text() value. It sounds like that is a common problem in Selenium: https://stackoverflow.com/a/28948550/14419

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147