当我使用包含搜索元素的 text() 中是否存在数据时,它适用于纯数据,但不适用于元素内容中有回车符、新行/标签时。//td[contains(text(), "")]
在这种情况下如何工作?谢谢!
XML:
<table>
<tr>
<td>
Hello world <i> how are you? </i>
Have a wonderful day.
Good bye!
</td>
</tr>
<tr>
<td>
Hello NJ <i>, how are you?
Have a wonderful day.</i>
</td>
</tr>
</table>
Python :
>>> tdout=open('tdmultiplelines.htm', 'r')
>>> tdouthtml=lh.parse(tdout)
>>> tdout.close()
>>> tdouthtml
<lxml.etree._ElementTree object at 0x2aaae0024368>
>>> tdouthtml.xpath('//td/text()')
['\n Hello world ', '\n Have a wonderful day.\n Good bye!\n ', '\n Hello NJ ', '\n ']
>>> tdouthtml.xpath('//td[contains(text(),"Good bye")]')
[] ##-> But *Good bye* is already in the `td` contents, though as a list.
>>> tdouthtml.xpath('//td[text() = "\n Hello world "]')
[<Element td at 0x2aaae005c410>]