An excellent conversation popped up on Slack about XPaths vs CSS selectors recently.
One member said
It’s more powerful than CSS. There are things that Xpath can do that CSS cannot. You can do things like find text containing etc… I have used it before for a particularly complicated cases where I needed to find all the specific elements after a certain element number (It was a dynamic site and had a draggable row, that was coded in html to be similar to the other rows but slightly different. Can’t remember the exact use case but it was not remotely possible in CSS)
I think… Xpath gets a bad rep… because people don’t read too much into it, and see horrible absolute xpaths copied directly out of chrome that look like this
I agree that is horrible . But that is not good xpath.
it gets a bad rep because most examples are copied directly out of the browser.
I am genuinely suprised, that CSS seems to be the dominant selector over xpath, I feel had we better educational resources on xpath, it would be the dominant selector as you can do more with it
A better answer would probably list what you can do in xpath vs CSS
Another member said
i see the point of the bigger feature set. but the need to use these features is a smell to me, a bandaid on the underlying problem that your UI isn’t well structured, the html not very descriptive.
also the smaller feature set and less special characters makes selectors better to learn, read and maintain in my opinion.
Both posters agreed that they’d seen some very poorly written CSS selectors over the years too though.
What do you think? Is XPath a UI smell?