baudehlo / node-phantom-simple

Simple bridge to phantomjs for Node
MIT License
201 stars 70 forks source link

"page.content" is underfined? #114

Closed wspl closed 8 years ago

wspl commented 8 years ago

When I try to get source code from phantomjs, page.content is underfined.

driver.create({ path: phantomjs.path }, function (err, browser) {
  return browser.createPage(function (err, page) {
    return page.open("http://tilomitra.com/repository/screenscrape/ajax.html", function (err, status) {
      console.log(page.content);
    });
  });
});
Reewr commented 8 years ago

Unlike PhantomJS, the page object in node-phantom-simple does not have any properties. In order to get what would be `page.content`` in PhantomJS, you will need to do the following:

page.get('content', function(err, content) {
  // content can be used here.
});

I hope that helps. This is true for all properties that can be found on phantom's api pages

wspl commented 8 years ago

@Reewr Another weird problem. I was trying to get rendered html from http://music.163.com/#/song?id=30841216 but when I usepage.get('content'...) it only return source-code without javascript render.(e.g. a iframe rendered by javascript is empty)

driver.create({ path: phantomjs.path }, (err, browser) => {
  browser.createPage((err, page) => {
    page.open('http://music.163.com/#/song?id=30841216', (err, stat) => {
      page.get('content', (err, content) => {
        console.log('1: ' + stat);
        console.log(content);
      });
    })
  });
});

The iframe tag in content:

<iframe name="contentFrame" id="g_iframe" class="g-iframe" scrolling="auto" frameborder="0" src="about:blank"></iframe>

In chrome: image

Thanks.

wspl commented 8 years ago

@Reewr Solved! Sorry for the redundant At.

driver.create({
  path: phantomjs.path,
  parameters: {
    'web-security': false
  }
}, (err, browser) => {
  browser.createPage((err, page) => {
    page.open('http://music.163.com/#/song?id=30841216', (err, stat) => {
      page.switchToFrame('contentFrame');
      page.get('frameContent', (err, content) => {
        console.log('1: ' + stat);
        console.log(content);
      });
  });
});