phantomjs not waiting for “full” page load

前端 未结 14 1176
南旧
南旧 2020-11-22 13:48

I\'m using PhantomJS v1.4.1 to load some web pages. I don\'t have access to their server-side, I just getting links pointing to them. I\'m using obsolete version of Phantom

14条回答
  •  执念已碎
    2020-11-22 14:42

    You could try a combination of the waitfor and rasterize examples:

    /**
     * See https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js
     * 
     * Wait until the test condition is true or a timeout occurs. Useful for waiting
     * on a server response or for a ui change (fadeIn, etc.) to occur.
     *
     * @param testFx javascript condition that evaluates to a boolean,
     * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
     * as a callback function.
     * @param onReady what to do when testFx condition is fulfilled,
     * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
     * as a callback function.
     * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
     */
    function waitFor(testFx, onReady, timeOutMillis) {
        var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
            start = new Date().getTime(),
            condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()), //< defensive code
            interval = setInterval(function() {
                if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
                    // If not time-out yet and condition not yet fulfilled
                    condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
                } else {
                    if(!condition) {
                        // If condition still not fulfilled (timeout but condition is 'false')
                        console.log("'waitFor()' timeout");
                        phantom.exit(1);
                    } else {
                        // Condition fulfilled (timeout and/or condition is 'true')
                        console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                        typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
                        clearInterval(interval); //< Stop this interval
                    }
                }
            }, 250); //< repeat check every 250ms
    };
    
    var page = require('webpage').create(), system = require('system'), address, output, size;
    
    if (system.args.length < 3 || system.args.length > 5) {
        console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
        console.log('  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
        phantom.exit(1);
    } else {
        address = system.args[1];
        output = system.args[2];
        if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
            size = system.args[3].split('*');
            page.paperSize = size.length === 2 ? {
                width : size[0],
                height : size[1],
                margin : '0px'
            } : {
                format : system.args[3],
                orientation : 'portrait',
                margin : {
                    left : "5mm",
                    top : "8mm",
                    right : "5mm",
                    bottom : "9mm"
                }
            };
        }
        if (system.args.length > 4) {
            page.zoomFactor = system.args[4];
        }
        var resources = [];
        page.onResourceRequested = function(request) {
            resources[request.id] = request.stage;
        };
        page.onResourceReceived = function(response) {
            resources[response.id] = response.stage;
        };
        page.open(address, function(status) {
            if (status !== 'success') {
                console.log('Unable to load the address!');
                phantom.exit();
            } else {
                waitFor(function() {
                    // Check in the page if a specific element is now visible
                    for ( var i = 1; i < resources.length; ++i) {
                        if (resources[i] != 'end') {
                            return false;
                        }
                    }
                    return true;
                }, function() {
                   page.render(output);
                   phantom.exit();
                }, 10000);
            }
        });
    }
    

提交回复
热议问题