Amazonの書籍のプレビューコンテンツ(PNGイメージまたはHTMLコンテンツのいずれか)を必要とするプロジェクトを少し実行しています。Amazon iframeでCasperJSが動作しない
たとえば、この本の内容はhttps://www.amazon.com/gp/product/B00JNYEXCK/です。
「内側を見る」バッジ(id = "sitbLogoImg"のimgタグ)をクリックすると、このフレームのプレビューコンテンツを示す新しいフレームが表示されます。それは2つのバージョン、印刷されたプレビュー(これはPNG画像、これらは私が保持することができます)と光り輝くプレビュー(これはiframeドキュメントです)を持っています。
私は基本的にはこのようになりますこれは、Kindleのプレビュー用のiframeとこだわっている:ここに私のCasperJSスクリプトです
<div id="scrollElm-0" class="pageHtml">
<div id="sitbReaderKindleSample">
<iframe id="sitbReaderFrame">
<html>
<head></head>
<body>
<p>.......</p>
<div>......</div>
....
</body>
</html>
</iframe>
</div>
</div>
:
var fs = require('fs');
var casper = require('casper').create({
pageSettings: {
loadPlugins: false,
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'
}
});
casper.options.viewportSize = {
width: 1366,
height: 768
};
casper.options.waitTimeout = 10000;
// use any cookies
var cookieFilename = "cookies.txt";
var data = fs.read(cookieFilename);
if (data) {
phantom.cookies = JSON.parse(data);
}
casper.start('https://www.amazon.com/gp/product/B00JNYEXCK/', function() {
this.echo(this.status(true));
this.captureSelector('before.png', 'html');
});
casper.waitForSelector('img#sitbLogoImg', function() {
//this.captureSelector('before.png','html');
});
casper.then(function() {
this.click('img#sitbLogoImg');
});
casper.waitForSelector('div#sitbLBHeader', function() {
});
var lis_content = '';
casper.wait(3000, function() {
this.captureSelector('after.png', 'html');
});
casper.withFrame(1, function() {
lis_content = this.getHTML();
this.captureSelector('lis_content.png', 'html');
});
//Write the sitbReaderFrame to file
casper.then(function() {
var lis_content_filename = 'lis_content.html';
fs.write(lis_content_filename, lis_content, 644);
});
// write the cookies
casper.wait(1000, function() {
var cookies = JSON.stringify(phantom.cookies);
fs.write(cookieFilename, cookies, 644);
});
casper.run();
iframeはid = "sitbReaderFrame"しか持たず、名前がないので、私はcasperjs.withFrameを0から4のフレームインデックス番号で試しましたが、CapserJSビューで終了しないようです。
私は本当にここにこだわっているので、あなたからのアドバイスをお聞きしたいと思います。どうもありがとうございました。
が役に立つことがあります。[JavaScriptを使用してiframeのスクロール?](http://stackoverflow.com/questions/1192228/scrolling-an-iframe-with-javascript) | [CasperJS ScrollTo](http://docs.casperjs.org/en/latest/modules/casper.html#scrollto) –