How to combine XPATH “select //td/text() or //td/p/text()” into one sentence?
-
12-06-2021 - |
Question
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>
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow