2016-11-12 18 views
1

我想要獲取CasperJS中多級iframe中的所有鏈接。有一個solution來解決有一個級別的iframe的情況。我試圖把getLinksFromIFrame放在getLinksFromIfram中來做遍歷遍歷,但失敗了。使用CasperJS中的函數返回多級iframe中的鏈接

對於此代碼,我應該如何處理多級iframe?

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(); 

回答

1

也許是這樣的:

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(); 

注:
現在我們有一個完整的多拉特。

./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 
+0

看來,我們現在有一個完整的遞歸。 我已經創建了2個iframe的節點1:第一個有一個鏈接,以及兩個iframe,每個鏈接都有一個鏈接,第二個鏈接有一個鏈接,以及一個iframe和一個鏈接。一切似乎都很好。 – 2016-11-13 06:38:39

+0

我希望這個腳本能夠按預期工作,但也許需要一些改進。我懷疑,如果** Artjom B。**離開我們? – 2016-11-14 00:20:10

+1

非常感謝,這段代碼令人驚歎。 – Yijin