Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PhantomJs示例不起作用?_Javascript_Phantomjs_Casperjs - Fatal编程技术网

Javascript PhantomJs示例不起作用?

Javascript PhantomJs示例不起作用?,javascript,phantomjs,casperjs,Javascript,Phantomjs,Casperjs,有人能告诉我为什么下面链接中的示例不起作用吗。我甚至把超时时间增加到12秒。始终显示我=>console.log('waitFor()'timeout') 链接: 我复制了这个例子,但不起作用。我正在尝试使用“phantomjs”运行它。有人能告诉我可能的原因吗 PhantomJS版本:2.1.1我已经修改了这个脚本,现在它可以工作了: /** * Wait until the test condition is true or a timeout occurs. Useful for wai

有人能告诉我为什么下面链接中的示例不起作用吗。我甚至把超时时间增加到12秒。始终显示我=>console.log('waitFor()'timeout')

链接:

我复制了这个例子,但不起作用。我正在尝试使用“phantomjs”运行它。有人能告诉我可能的原因吗


PhantomJS版本:2.1.1

我已经修改了这个脚本,现在它可以工作了:

/**
 * 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.
 */

"use strict";
function waitFor(testFx, onReady, timeOutMillis) {
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 5000, //< 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
};


function click(sel){var event=document.createEvent('MouseEvents');event.initMouseEvent('click',1,1,window,1,0,0,0,0,0,0,0,0,0,null);document.querySelector(sel).dispatchEvent(event);}
var page = require('webpage').create();

// Open Twitter on 'sencha' profile and, onPageLoad, do...
page.open("https://twitter.com/sencha", function (status) {
    // Check for page load success
    if (status !== "success") {
        console.log("Unable to access network");
    } else {
       page.evaluate(function(click) {click('a[id="signin-link"]');},click)//,click will be available
        // Wait for 'signin-dropdown' to be visible
        waitFor(function() {
            // Check in the page if a specific element is now visible
            return page.evaluate(function() {
                return $("div#signin-dropdown").is(":visible");
            });
        }, function() {
           console.log("The sign-in dialog should be visible now.");
           phantom.exit();
        });
    }
page.render('sencha.png'); 
});
/**
*等待测试条件为真或出现超时。等待有用
*在服务器响应或发生ui更改(fadeIn等)时。
*
*@param testFx计算为布尔值的javascript条件,
*它可以作为字符串传入(例如:“1==1”或“$”(“#bar”)。is(“:visible”)”或
*作为回调函数。
*@param onReady满足testFx条件时该怎么办,
*它可以作为字符串传入(例如:“1==1”或“$”(“#bar”)。is(“:visible”)”或
*作为回调函数。
*@param timeOutMillis是等待的最大时间。如果未指定,则使用3秒。
*/
“严格使用”;
函数waitFor(testFx、onReady、timeOutMillis){
var maxtimeOutMillis=timeOutMillis?timeOutMillis:5000,//<默认最大超时为3s
开始=新日期().getTime(),
条件=假,
间隔=设置间隔(函数(){
如果((新日期().getTime()-start
现在我们发送一个点击按钮,打开下拉菜单并使其可见。Meta-comment:嗨,伊戈尔,我注意到你有时会删除你没有收到任何反馈的答案。请不要这样做。如果没有其他更好的答案,那么你的答案可能会对未来的读者有所帮助。我还可以说,有些提问者实际上忘记了他们问了一个问题,有时会在很晚的时候重新讨论他们的问题。在我发布这些问题一年多后,我的答案已经被接受。请访问并查看它们,看看是否可以取消删除其中的一些。