Giter Site home page Giter Site logo

nightmare-xpath's Introduction

nightmare-xpath

Nightmare plugin to select nodes by xpath expression.

INSTALL

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

USAGE

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']

API

xpath(selector: string, handler?: (Node: node) => any): Array<any>

selector

Type: string

XPath expression.

handler

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.

RESOURCES

nightmare-xpath's People

Contributors

semantic-release-bot avatar unlight avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

vtange

nightmare-xpath's Issues

xpath does not support promises

I am using Javascript ES6 and I use the promises quite a lot, they've been integrated to Nightmare and it would be nice that they are integrated here too.

Error: TypeError: self.goto(...).xpath(...).then is not a function

Suggested Change to how Nightmare prototype is changed?

Hey I love this package and that I can select off the xpath of an element. However, I happen to use a lot of different modules that modify the Nightmare instance that I use to scrape, and it's hard to remember how all the different packages change the Nightmare class. instead of doing the following:

var Nightmare = require("nightmare");
// all the function definitions
Nightmare.prototype.xpath = function(selector, handler) {}

Maybe it could be changed to the following:

module.exports = exports = function(Nightmare) {
  // all the function definitions
  Nightmare.prototype.xpath = function(selector, handler) {}
}

Without the change, my require statements look like the following:

/** Require in all my node modules and npm modules **/
// Most of the work I do for my clients is scraping portals, downloading files, and other sundry task automation that they need but don't want to do so I use packages like csvtojson, url parsers, path, promisify, etc. so the require statements are usually long

/** Build the Nightmare Instance that I want to Use **/
let Nightmare = require('Nightmare');
Nightmare = require('some-other-nightmare-pkg')(Nightmare);
require('nightmare-inline-download')(Nightmare);
require('nightmare-xpath');

It is frustrating to have to go and research how each particular module modifies Nightmare each time I am going to write a new scrape. If nightmare-xpath included the change above though then my require statements could be simplified a lot:

/** Require in my node and npm modules **/

/** Build the custom Nightmare for this particular scrape **/
const Nightmare = require('Nightmare');
require('some-other-nightmare-pkg')(Nightmare);
require('nightmare-inline-download')(Nightmare);
require('nightmare-xpath')(Nightmare);

// do scraping and automation task

Just a thought. Would you like me to put it in a pull request??

this._evaluate is not a function

Hello,

Came across your library while looking for a way to use XPATH with nightmarejs. Got the above error when trying to use nightmare-xpath 2.0.1 with nightmare 2.8.1. Looks to be a missing method on the nightmare prototype.

After some poking around in the nightmarejs code, I found a method called evaluate_now. Changed line 26 of nightmare-xpath's index.js from '_evaluate' to 'evaluate_now' on my local and everything's working again.

Don't know if nightmarejs changed - but this may help someone else. Let me know if you want to go this way and I can submit a pull request after re-running tests

Thanks!

Latest version of nightmare.js for ref:
https://github.com/segmentio/nightmare/blob/master/lib/nightmare.js

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.