How to wait for a click() event to load in phantomjs before continuing?

后端 未结 3 1574
夕颜
夕颜 2020-12-14 19:48

Phantomjs has these two really handy callbacks onLoadStarted and onLoadFinished which allow you to essentially pause execution while the page is lo

3条回答
  •  爱一瞬间的悲伤
    2020-12-14 20:07

    Here is my code based on some other answers. In my case, I didn't need to specifically evaluate any other javascript. I just needed to wait for the page to finish loading.

    var system = require('system');
    if (system.args.length === 1) {
        console.log('Try to pass some arguments when invoking this script!');
    }
    else {
        var page = require('webpage').create();
        var address = system.args[1];
    
        page.open(address, function(status){
            page.onLoadFinished = function(status) {
                console.log(page.content);
                phantom.exit();
            };    
        });     
    }
    

    Save the above in a file called "scrape.js" and call it this way:

    phantomjs --ssl-protocol=any --ignore-ssl-errors=true scrape.js https://www.example.com
    

    The SSL-related params are added to avoid other issues that I was having with certain HTTPS sites (related to certificate loading issues).

    Hope this helps someone!

提交回复
热议问题