Javascript PhantomJS:如何在网页上执行函数

Javascript PhantomJS:如何在网页上执行函数,javascript,phantomjs,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.in

我需要执行简单的脚本剪辑网站上的按钮。经过一些研究和阅读文档后,我编写了以下脚本:

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新手,请帮我找出问题出在哪里。

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

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

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

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
};
函数waitFor(testFx、onReady、timeOutMillis){
var maxtimeOutMillis=timeOutMillis?timeOutMillis:3000,//<默认最大超时为3s
开始=新日期().getTime(),
条件=假,
间隔=设置间隔(函数(){
如果((新日期().getTime()-start
您看到退出前的测试超时输出了吗?没有,只是因为它在网页上执行,而不是在PhantomJS控制台中执行。删除它。感谢您的快速响应,我将phantom.exit()调用放在setTimeout函数中,现在我得到了“TypeError:“undefined”不是函数(评估“anchor.click()”)”更新了代码顺便说一句,我忘记了bump_trades()函数。感谢您的响应!我会一步一步地找出你的代码在做什么。现在我只是复制粘贴它,并得到相同的错误“TypeError:“undefined”不是一个函数(计算'anchor.click()')”。有什么建议吗?您可以在页面上下文中用于
console.log
调试。
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
};