2017-06-01 94 views
0

我是一個新手,最近開始與casperjs一起使用phantomjs。我想從iframe獲取信息,但phantomjs無法加載它。Phantomjs無法加載iframe

這是我的腳本:

var casper = require('casper').create({ 
    verbose: true, 
    logLevel: "debug", 
    waitTimeout: 20000, 
    retryTimeout: 100, 
    viewportSize: { 
    width: 1920, 
    height: 1080 
    }, 
    pageSettings: { 
     "userAgent": 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1' 
    }, 
    localToRemoteUrlAccessEnabled: true 
}); 

casper.start(); 

casper.open('http://www.badboysbarber.ru/online'); 

casper.waitForSelector('.y-main-container', function() { 
    this.echo("Selector appeared."); 
}); 

casper.then(function() { 
    this.capture('screen.png'); 
}); 

casper.run(); 

因此,幻影拋出一個錯誤(雖然選擇正確定義):

[error] [phantom] Wait timeout of 20000ms expired, exiting. 

可能有人幫助我嗎?也許我做錯了什麼?謝謝。

回答

0

iframe將文檔加載到另一個文檔中。如果您想使用框架並使用CasperJS獲取數據,那麼您可能需要withFrame()中的Casper.prototype

下面的腳本捕捉你的主頁的第一iframe的原始HTML內容:

var casper = require('casper').create({ 
    viewportSize: { 
    width: 1920, 
    height: 1080 
    }, 
    pageSettings: { 
    'userAgent': 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1' 
    }, 
    localToRemoteUrlAccessEnabled: true 
}); 

casper.start('http://www.badboysbarber.ru/online'); 

casper.withFrame(0, function() { 
    this.echo(this.getHTML()); // HTML code of the first iframe 
}); 

casper.run(); 
+0

很抱歉,但即使你使用withFrame功能,它無法加載一個iframe。試着使用'this.captureSelector('selector.png','.y-main-container');'而不是'this.echo(this.getHTML());'裏面的withFrame函數,你會看到。這是整個問題。 –

+0

此外,您還可以查看[此主題](https://groups.google.com/forum/#!topic/casperjs/AtbXjGnp7M0)。 –

+0

如果您可以獲取HTML代碼,那麼'iframe'顯然會加載。你只有'capture'的問題。我用SlimerJS嘗試'this.capture('screenshot.png');'withFrame'內部',並且我可以看到'iframe',不僅在SlimerJS正在處理頁面時,而且在得到的屏幕截圖上:http:// imgur.com/a/gWtiY – Badacadabra