Skip to content

page.evaluate cloud not get any result #153

@willin

Description

@willin
  • osx 10.12.6
  • phantomjs-prebuild 2.1.14
  • node-phantom-simple 2.2.4
  • node 8.1.4
const driver = require('node-phantom-simple');
const phantom = require('phantomjs-prebuilt');

driver.create({
  path: phantom.path
}, (err, browser) => {
  browser.createPage((err2, page) => {
    page.open('https://www.google.com/', (err3, status) => {
      console.log('opened site? ', status);
      page.evaluate(() => document.getElementsByTagName('body')[0].innerHTML, (err4, result) => {
        console.log(result);
        browser.exit();
      });
    });
  });
});
opened site?  success
null

if i use:

page.get('content', function (err, html) {
  console.log("Page HTML is: " + html);
});

can print html result.

using basic example:

const driver = require('node-phantom-simple');

driver.create({ path: require('phantomjs-prebuilt').path }, (err, browser) => browser.createPage((err, page) => page.open('http://tilomitra.com/repository/screenscrape/ajax.html', (err, status) => {
  console.log('opened site? ', status);
  page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', (err) => {
    // jQuery Loaded.
    // Wait for a bit for AJAX content to load on the page. Here, we are waiting 5 seconds.
    setTimeout(() => page.evaluate(() => {
      // Get what you want from the page using jQuery. A good way is to populate an object with all the jQuery commands that you need and then return the object.
      let h2Arr = [],
        pArr = [];

      $('h2').each(function () { h2Arr.push($(this).html()); });
      $('p').each(function () { pArr.push($(this).html()); });

      return {
        h2: h2Arr,
        p: pArr
      };
    }, (err, result) => {
      console.log(result);
      browser.exit();
    }), 5000);
  });
})));
// opened site?  success
// null

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions