How to combine XPATH “select //td/text() or //td/p/text()” into one sentence?

StackOverflow https://stackoverflow.com/questions/10865111

  •  12-06-2021
  •  | 
  •  

문제

I want to select text() within each row in the following HTML. However, the text I want is either in the td element or the p element, so I have to write two statements to ensure each row is selected.

How do I combine the two statements into one?

XPATH:

//table/tr/td[not(p)]/text() | //table/tr/td/p/text()

With the result desired:

['1', '2', '3', '4']

Original html:

<table>
  <tr>
    <td>1</td>
  </tr>
  <tr>
    <td>
      <p>2
    </td>
  </tr>
  <tr>
    <td>3</td>
  </tr>
  <tr>
    <td>
      <p>4
    </td>
  </tr>
</table>
도움이 되었습니까?

해결책

Probably you want something like this:

//table/tbody/tr/td//text()[normalize-space()]

All non-whitespace-only text nodes one or more levels deep in the //table/tr/td will be found.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top