Reputation: 25
I just started out with Python and learning about xpath expressions. I'm trying to get a div, an a class, look for the href inside the a class and then get the part of the href, then just continue with something.
div class: dropdown-menu
and a class: dropdown-item
My url: https://www.something.com/library/category/stuff
My xpath expression: response.xpath("//div[@class='dropdown-menu']//a[@class='dropdown-item']//a[contains(@href, 'category')]")
It just returns an empty string and I can't figure out why, please advice.
Upvotes: 0
Views: 501
Reputation: 338208
Since an <a>
can't really be nested inside an <a>
, I suppose you meant to write two conditions for the same <a>
here:
response.xpath("//div[@class='dropdown-menu']//a[@class='dropdown-item']//a[contains(@href, 'category')]")
That would be written like this:
response.xpath("//div[@class='dropdown-menu']//a[@class='dropdown-item' and contains(@href, 'category')]")
or like this (predicates, i.e. the filter conditions in the square brackets, can be chained and are evaluated one after another):
response.xpath("//div[@class='dropdown-menu']//a[@class='dropdown-item'][contains(@href, 'category')]")
Upvotes: 1