$x() not part of the webpage runtime or script content. This is a tool that is part of the command line API for Chrome DevTools .
To use XPath in the content of the script, you need to do it in the usual way, the convenient DevTools shortcut is not available.
Your code will look like this:
var jpgLinks = document.evaluate ( "//a[contains(@href,'.jpg')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null ); var numLinks = jpgLinks.snapshotLength; for (var J = 0; J < numLinks; ++J) { var thisLink = jpgLinks.snapshotItem (J); console.log ("Link ", J, " = ", thisLink); }
is what $x() did for you, backstage.
While you're on it, go to the CSS selector . Then the same functionality:
var jpgLinks = document.querySelectorAll ("a[href$='.jpg']"); var numLinks = jpgLinks.length; for (var J = 0; J < numLinks; ++J) { var thisLink = jpgLinks[J]; console.log ("Link ", J, " = ", thisLink); }
- which is much nicer in my book.
Brock adams
source share