4 ms·
I just started getting into xpaths pretty hardcore with my trivia generator for http://playhattrick.com http://playhattrick.com ... I use it for identifying tab
by callmeed 13y ago
I just started getting into xpaths pretty hardcore with my trivia generator for http://playhattrick.com http://playhattrick.com ... I use it for identifying tables of data to scrape. It's not as fun as regex IMO but it is powerful.
Pro Tip: the chrome inspector lets you right-click on an element and get its xpath.
Pro Warning: sometimes the xpath generated by chrome doesn't work when scraping with Nokogiri. I'm not sure why yet, I've just learned not to rely on it.
- toyg 13y agoA problem with XPath is that many tools don't actually support the latest spec, or even a spec, and just give you a bastardised syntax that looks like XPath and walks like XPath but will never swim like XPath. This is frustrating because you're never sure which features you can actually rely on.
- garethadams 13y agoThere's not "an" XPath for an element, as XPath describes the route you take from the root of a document to the element in question. The correct route for your situation depends on your use case. Describing an element as "the first child of the fifth child of the second child of the first child of the eighth child of the second child of HTML" is as much the right path to an element as if you described the way to your house as "Walk past the park then walk past the bus stop then walk past the hardware store then walk past the butchers then turn left then walk past the pizza shop then walk past the library"
- callmeed 13y agoI'm aware what XPath is/does. And I'm not disagreeing with you–I'm only saying Chrome has this feature. I don't know what route they choose for you but I know they don't always work in tools that parse (HT/X)ML. Here's a screenshot in case you don't believe me: http://imgur.com/9FZSMSt http://imgur.com/9FZSMSt The XPath Chrome returns for this page is: //*[@id="details"]/article/table[1]
- riffraff 13y agoadditionally, chrome's confole also has a $x function to find elements by xpath