Sourdough Sourdough - 10 months ago 50
CSS Question

How do I select one of 5 child elements which has a unique class name in XPath?

In the linked screenshot below, it's a rating out of 5. If a 1 star rating is given by the user. The HTML looks like this:.

enter image description here

However, if let's say a four star rating is given the HTML looks like this:

enter image description here

I want to get the user rating so "1" or "4" repsctively in the screenhots above.

So far I have got this:

.//td[@class='review-rating-header seat_comfort']/following-sibling::td[@class='review-rating-stars stars']//span[contains(concat(' ', @class, ' '), ' fill ')]

This selects "1" for all rows even though user ratings maybe "2", "3", "4" or "5".

I'm web scraping by the way, if that helps.

Any ideas on how to get desired XPath element?

Answer Source

If you want the last span with class fill, try

span[contains(concat(' ', @class, ' '), ' fill ')][last()]