grigy
grigy

Reputation: 6826

Select deeply nested element

I'm reading Scrapy/XPath tutorials but this does not seem trivial and I can't find an example that would explain it.

Given a markup like this how would you select the <span> element?

<div id=”...”>
	<div>
	<div>
	<div>
		<div>
		<div>
			<div>
				<div>
					<span>

If we generalize the problem it would be:

Upvotes: 9

Views: 19566

Answers (1)

kjhughes
kjhughes

Reputation: 111491

Assuming indentation denotes containment in your example, the following XPath will select the span element for you:

//div[@id='...']/div[3]/div[2]/div/div/span

Of course, if there are no other span elements beneath the id'ed div, you could jump right to it:

//div[@id='...']//span

Or if there are no other span elements in the entire document:

//span

Upvotes: 19

Related Questions