使用CasperJS中的函数返回多级iframe内的链接 [英] Returning links inside multiple-levels iframe using a function in CasperJS

查看:126
本文介绍了使用CasperJS中的函数返回多级iframe内的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在CasperJS中获取多级iframe下的所有链接。有一个解决方案来解决那里的情况是一级iframe。我试图将getLinksFromIFrame放在getLinksFromIfram中进行重复遍历但是失败了。

I am trying to get all links under multiple level iframes in CasperJS. There was a solution to resolve the case that there is one-level iframe. I am trying to put getLinksFromIFrame inside the getLinksFromIfram to do recurive traverse but failed.

对于此代码,我应该如何为多级iframe执行此操作?

For this code, how should I do it for multiple-level iframes?

function getLinksFromIframes( callback ) {
    var links = [];

    var iframes = this.evaluate( function() {
        var iframes = [];
        [].forEach.call(document.querySelectorAll("iframe"), function(iframe, i) { iframes.push( i ); });
        return iframes;
    });

    iframes.forEach( function( index ) {
        this.withFrame(index, function() {
            links = links.concat( this.getElementsAttribute( 'a', 'href' ) );
            console.log("works: " + links);
        });
    }, this);

    this.then(function(){
        callback.call(this, links);
    });
}

casper.start(url, function () {
    getLinksFromIframes.call(this, function(links){
        thelinks = links;
        console.log("Links: " + thelinks);
    });
})
.then(function(){
    console.log("Links later: " + thelinks);
})
.run();


推荐答案

也许是这样:

var casper = require("casper").create({
        //    verbose: true,
        //    logLevel: "debug",
        webSecurityEnabled : false
    });
//page.onConsoleMessage = function(msg) {console.log(msg);};
casper.on('remote.message', function (message) {
    this.echo(message);
});

casper.start("http://domu-test-2/node/1", function () {
    this.evaluate(function () {
        var i,
        x = document.querySelector("iframe#test") //First iframe
            .contentDocument.querySelector("iframe#test2") //Second iframe in the first
            .contentDocument.querySelectorAll("a"); //Links
        for (i = 0; i < x.length; i++) {
            console.log(x[i].href)
        }
    })
}).wait(1000).run();

这很难,但我创建了这个脚本:

var casper = require("casper").create({
        //  verbose: true,
        //    logLevel: "debug",
        webSecurityEnabled : false
    });
var links = [];
function get_links(obj) {
    return obj.evaluate(function () {
        var i,
            l = document.querySelectorAll("a"),
            l2 = [];
        for (i = 0; i < l.length; i++) {
            l2[i] = l[i].href;
        }
        return l2
    });
}
function unique(arr) {
    var obj = {};
    for (var i = 0; i < arr.length; i++) {
        if (/http(.*)?/.test(arr[i])) {
            var str = arr[i];
            obj[str] = true;
        }
    }
    return Object.keys(obj);
}

function getLinksFromIframes(callback) {
    this.echo("Here we come: " + this.getCurrentUrl() + "\n");
    function to_frame(obj) {
        var iframes = to_evaluate(obj);
        iframes.forEach(function (index) {
            this.withFrame(index, function () {
                this.echo("We are here: " + this.getCurrentUrl());
                var l = unique(get_links(this));
                var i;
                for (i = 0; i < l.length; i++) {
                    console.log(l[i]);
                    links.push(l[i])
                }
                links = unique(links);
                console.log("");
                to_frame(this) //multi lvl
            }); //The first iframe
        }, obj);
    }
    function to_evaluate(obj) {
        return obj.evaluate(function () {
            var iframes = [];
            [].forEach.call(document.querySelectorAll("iframe"), function (iframe, i) {
                iframes.push(i);
            });
            return iframes;
        })
    }
    to_frame(this);
    this.then(function () {
        callback.call(this);
    });
}

casper.start("http://domu-test-2/node/1", function () {
    getLinksFromIframes.call(this, function () {
        console.log("Done!\n");
        var i;
        for (i = 0; i < links.length; i++) {
            console.log(links[i]);
        }
    });
}).then(function () {}).run();

注意:

现在我们有一个完整的多个lvl。

Note:
Now we have a full multi lvl.

./casperjs test.js >>/dev/stdout
Here we come: http://domu-test-2/node/1

We are here: http://domu-test-2/node/2
http://link_1_inside_iframe(1.1)_from_main_frame

We are here: http://domu-test-2/node/3
http://link_1_inside_iframe(2.1)_from_1.1

We are here: http://domu-test-2/node/5
http://link_1_inside_iframe(2.2)_from_1.1

We are here: http://domu-test-2/node/4
http://link_1_inside_iframe(1.2)_from_main_frame

We are here: http://domu-test-2/node/6
http://link_1_inside_iframe(2.1)_from_1.2

这篇关于使用CasperJS中的函数返回多级iframe内的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆