2

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>

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.