【问题标题】:Phantomjs integrating waitfor with includeJsPhantomjs 将 waitfor 与 includeJs 集成
【发布时间】:2015-05-19 20:52:08
【问题描述】:

我可以使用示例中提供的 waitfor 函数 https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js 我也可以使用这里提供的 includeJs http://phantomjs.org/page-automation.html

我很难弄清楚如何将 jquery 包含在 waitfor 函数中。

下面的 sudo 代码只是我尝试过的众多方法之一。请帮忙。

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
};


var page = require('webpage').create();

page.open("http://example.com", function (status) {
    if (status !== "success") {
        console.log("Unable to access network");
    } else {
        waitFor(function() {
            // This does not work
            page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
                return page.evaluate(function() {
                    return $("#some-element").is(":visible");
                });
            });    
        }, function() {
           console.log("some-element should be visible now.");
           phantom.exit();
        });        
    }
});

【问题讨论】:

    标签: javascript jquery phantomjs


    【解决方案1】:

    正如您在waitFor() 代码中看到的,testFx 函数每 250 毫秒调用一次。该函数应该只包含实际的检查代码而不是加载代码。只需将includeJs() 调用移出testFx 函数即可。另请注意,testFx 函数必须返回您的示例中没有返回的内容,因此它始终未定义并在一段时间后超时。

    page.open("http://example.com", function (status) {
        if (status !== "success") {
            console.log("Unable to access network");
        } else {
            page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
                waitFor(function() {
                    return page.evaluate(function() {
                        return $("#some-element").is(":visible");
                    });
                }, function() {
                   console.log("some-element should be visible now.");
                   phantom.exit();
                });
            });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      相关资源
      最近更新 更多