问题
I am trying to grab the li
class text in the following based on the strong tag higher up in the tree containing a certain word, in this case: "restaurants":
<p class="">The location, where the condo is situated,
offers a good choice of <strong>restaurants</strong>.
Some of them are listed below:</p>
<ul class="">
<li class="">Restaurant 1</li>
<li class="">Restaurant 2</li>
<li class="">Restaurant 3</li>
<li class="">Restaurant 4</li>
<li class="">Restaurant 5</li>
</ul>
I have tried many variations of the following, assuming that I need to go higher up the tree by using "ancestors":
//ul/li[contains(ancestor-or-self::@strong[contains(text(),'Restaurants')])
回答1:
This XPath,
//p[strong = 'restaurants']/following-sibling::ul[1]/li
will select all li
elements of the first ul
element following sibling of the p
element that contains a strong
element whose string value is "restaurants"
.
来源:https://stackoverflow.com/questions/45724510/grab-following-siblings-in-xpath