0
我正在运行一个需要某些亚马逊书籍的预览内容(它们可以是PNG图像或html内容)的小项目。CasperJS不适用于亚马逊iframe
例如,这本书:https://www.amazon.com/gp/product/B00JNYEXCK/。
当点击“Look inside”徽章(id =“sitbLogoImg”的img标签)时,会出现一个新框架,显示本书的预览内容。它有2个版本,打印预览(这是PNG图像,这些我可以抓住)和点燃预览(这是iframe文件)。
我坚持使用iframe针对Kindle预览,基本上是这样的:
<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>
这里是我的CasperJS脚本:
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) – 2016-12-30 15:08:51