Use “window.onload” in phantomjs

被刻印的时光 ゝ 提交于 2019-12-04 20:20:24

page.open(url, function(status){...}) is just another notation for

page.onLoadFinished = function(status){...};
page.open(url);

You can find the quote here:

Also see WebPage#open for an alternate hook for the onLoadFinished callback.


Since this is an AJAX-based page, you need to wait for the data to appear. You can only do that by repeatedly checking a specific portion of the page.

You can find an example in the examples directory of the phantomjs installation or here. This will probably also work for phantomjs through npm-phantom.

In your case this will look like this (abbreviated):

page.open(pref + url, function (status) {
   waitFor(function check(){
       return page.evaluate(function () {
           // ensure #first and #last are in the DOM
           return !!document.getElementById("first") && 
                  !!document.getElementById("last");
       });

   }, function onReady(){
       page.evaluate(function () {
           var data = {};
           data.one = document.getElementById("first").innerText;
           data.two = document.getElementById("last").innerText;
           return data;
        });
        callback(null, res);
        ph.exit();
   }, 5000); // some timeout
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!