van
van

Reputation: 9449

xpath selectors

I have the following HTML:

<ul>
<li>
<p class="channel-show-time">Test 1</p>
</li>
<li>
<p class="channel-show-time">Test 2</p>
</li>
<li><span class="channel-show-carousel-label">Next</span>
<p class="channel-show-time">Test 3</p>
</li>
<li>
<p class="channel-show-time">Test 4</p>
</li>
</ul>

I want to select the text in the <p> tags from the preceding li to the li with span class 'channel-show-carousel-label' so I want the text 'Test 2'.

I have the xpath that selects the text in the <p> tag for the li with the span class, i.e:

xpath=//ul/li/span[@class='channel-show-carousel-label']/../p

Does anyone know how I can achieve this?

Upvotes: 0

Views: 281

Answers (1)

choroba
choroba

Reputation: 241818

You can use the following XPath:

//span[@class="channel-show-carousel-label"]/../preceding-sibling::li[1]/p/text()

It says: find the span with the desired class, go to its parent (li), find the nearest preceding li sibling, go to its p child and return its text.

Upvotes: 2

Related Questions