I have some HTML like this (which I cannot change):
<div>
<p class="name">
<span>Employee Name: </span>
John Smith
</p>
</div>
And I’d like to use xpath to extract out just the “John Smith” part..
I have been trying to use this code:
//div//p[@class='name']//text()
However, it doesn’t work.
What is the best way to achieve what I’m after?
Many thanks.
You almost have it.
Change your XPath to:
//div//p[@class='name']/text()When you use
//text(), it selects all of the descendanttext()nodes, which includes the “Employee Name: ” text node that is a child of the<span>.It is best to avoid the
//when possible, as it makes your expressions less efficient and more prone to those sort of issues.