PhantomJS:如何在网页上执行功能

时间:2014-05-26 14:32:55

标签: javascript phantomjs

我需要执行简单的脚本来隐藏网站上的按钮。经过一些研究和阅读文档,我制作了这个脚本:

page.open('http://www.somewebsite.com/', function (status) {

    if (status !== "success") {
        console.log("Unable to access network");
    } else {
        console.log("Page loaded");

        page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {

            page.evaluate (function() {

                function bump_trades() {

                    var xpr = document.evaluate(".//li/a[@class='trade_bump']/div[@class='icon_bump']", document, null, XPathResult.ANY_TYPE, null);
                    var anchor;

                    while (anchor = xpr.iterateNext()) {
                        anchor = anchor.parentNode;                     
                        if (anchor.getAttribute('data-tradeid'))
                            break;
                    }
                    if (anchor && anchor.getAttribute('data-tradeid')) {
                        setTimeout(function(){
                                anchor.click();
                                setTimeout(bump_trades, 500);                               
                            }, 500);
                    } else {
                    }               
                };  
                bump_trades();
            }); 
            console.log('Exit');
            phantom.exit();
        });
    };  
}); 

浏览器控制台中的脚本本身(从var xpr ...开始)工作得很好,没问题。但是在PhantomJS中它什么都不做。我有控制台消息,页面已加载,但脚本不执行。 我是Javascript的新手,请帮我弄清问题在哪里。

2 个答案:

答案 0 :(得分:2)

您正在设置超时,但在实际执行之前您正在退出。在超时时移动phantom.exit()调用。

答案 1 :(得分:0)

当页面上下文中的长时间运行的函数终止时,您可以添加一个设置为true的变量。然后,您可以使用 waitFor 构造在幻像上下文中等待退出。所以改变

page.evaluate (function() {
    function bump_trades() {
        // your code        
    };  
    bump_trades();
}); 
console.log('Exit');
phantom.exit();

page.evaluate (function() {
    function bump_trades() {
        var xpr = document.evaluate(".//li/a[@class='trade_bump']/div[@class='icon_bump']", document, null, XPathResult.ANY_TYPE, null);
        var anchor;

        while (anchor = xpr.iterateNext()) {
            anchor = anchor.parentNode;                     
            if (anchor.getAttribute('data-tradeid'))
                break;
        }
        if (anchor && anchor.getAttribute('data-tradeid')) {
            setTimeout(function(){
                    anchor.click();
                    setTimeout(bump_trades, 500);                               
                }, 500);
        } else {
            window.bumpTradesCompleted = true;
        }               
    };  
    window.bumpTradesCompleted = false;
    bump_trades();
});
waitFor(function testFx(){
    return page.evaluate(function(){
        return window.bumpTradesCompleted;
    });
}, function onReady(){
    phantom.exit();
}, 1000000); // wait a long time

waitFor函数如下所示:

function waitFor(testFx, onReady, timeOutMillis) {
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
        start = new Date().getTime(),
        condition = false,
        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
};
相关问题