Nightmare plugin to select nodes by xpath expression.
This will install plugin nightmare-xpath
for Nightmare 3.X:
npm install --save-dev nightmare-xpath
Note: If you are looking for plugin for Nightmare 1.X, then use:
npm install nightmare-xpath@1
const Nightmare = require('nightmare');
require('nightmare-xpath')(Nightmare);
const links = await Nightmare()
.goto('http://example.com/')
.xpath('//a[@href]', function (node) {
// We cannot return DOM element to nodejs,
// we must return serializable object or primitive.
// If function is omitted, node.toString() will be used.
return node.href;
})
.then();
console.log(links); // ['https://www.iana.org/domains/example']
xpath(selector: string, handler?: (Node: node) => any): Array<any>
Type: string
XPath expression.
Type: function
Optional: Yes
Signature: (Node: node) => any
This function be called on each result of XPathResult
.
We cannot return DOM element to nodejs,
we must return serializable object or primitive.
If function is omitted, node.toString() will be used.