Issue
I want to select all elements article
that don't contain a span
element with class status
and where the nested a
element contains a href
attribute which contains the text "rent.html".
I've managed to get the a
element like so:
response.xpath('//article[@class="car"]//a[contains(@href,"rent.html")]')
But reading here and trying to select the first parent element article
like so returns "data=0"
response.xpath('//article[@class="car"]//a[contains(@href,"rent.html")]//parent::article and not //article[@class="car"]//span[@class="status"]')
I also tried this.
response.xpath('//article[@class="car"][//a[contains(@href,"rent.html")]/article and not //article[@class="car"]//span[@class="status"]')')
I don't know what the expression is for my use case.
<article class="car">
<div>
<div class="container">
<a href="/34625030/rent.html">
</a>
</div>
</div>
</article>
<article class="car">
<div>
<div class="container">
<a href="/34625230/rent.html">
</a>
</div>
</div>
</article>
<article class="car">
<div>
<div class="container">
<a href="/12325230/buy.html">
</a>
</div>
</div>
</article>
<article class="car">
<div>
<div class="container">
<a href="/34632230/rent.html">
</a>
</div>
</div>
<span class="status">Rented</span>
</article>
Solution
This XPath expression will do the work:
"//article[not(.//span[@class='status'])][.//a[contains(@href,'rent.html')]]"
The entire command is:
response.xpath("//article[not(.//span[@class='status'])][.//a[contains(@href,'rent.html')]]")
Explanations:
Translating your requirements into XPath syntax.
"select all elements article
" - //article
"that don't contain a span element with class status" - [not(.//span[@class='status'])]
" and where the nested a element contains a href attribute which contains the text "rent.html"" - [.//a[contains(@href,'rent.html')]]
I tested the XPath above on the shared sample XML and it worked properly.
Answered By - Prophet
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.