使用phantom.js切换到iframe
我想使用纯phantom.js代码切换到iframe 这是我的第一次尝试使用phantom.js切换到iframe,iframe,phantomjs,Iframe,Phantomjs,我想使用纯phantom.js代码切换到iframe 这是我的第一次尝试 var page=新网页(); var url='1〕http://www.theurltofectch' 页面打开(url、函数(状态){ 如果('success'!==状态){ 控制台日志(“错误”); }否则{ 第页切换到框架(“框架名称”); console.log(page.content); phantom.exit(); } }); 它只生成主页的源代码。有什么想法吗 请注意,iframe域与主页域不同。请
var page=新网页();
var url='1〕http://www.theurltofectch'
页面打开(url、函数(状态){
如果('success'!==状态){
控制台日志(“错误”);
}否则{
第页切换到框架(“框架名称”);
console.log(page.content);
phantom.exit();
}
});
它只生成主页的源代码。有什么想法吗
请注意,iframe域与主页域不同。请尝试一下。我相信这可能是一个异步问题,意味着在尝试访问iframe时,iframe不存在。我从另一篇文章中收到了下面的片段
var page = require('webpage').create(),
testindex = 0,
loadInProgress = false;
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
/*
page.onNavigationRequested = function(url, type, willNavigate, main) {
console.log('Trying to navigate to: ' + url);
console.log('Caused by: ' + type);
console.log('Will actually navigate: ' + willNavigate);
console.log('Sent from the page\'s main frame: ' + main);
};
*/
/*
The steps array represents a finite set of steps in order to perform the unit test
*/
var steps = [
function() {
//Load Login Page
page.open("https://www.yourpage.com");
},
function() {
//access your iframe here
page.evaluate(function() {
});
},
function() {
//any other step you want
page.evaluate(function() {
});
},
function() {
// Output content of page to stdout after form has been submitted
page.evaluate(function() {
//console.log(document.querySelectorAll('html')[0].outerHTML);
});
//render a test image to see if login passed
page.render('test.png');
}
];
interval = setInterval(function() {
if (!loadInProgress && typeof steps[testindex] === "function") {
console.log("step " + (testindex + 1));
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] !== "function") {
console.log("test complete!");
phantom.exit();
}
}, 50);
替换
console.log(page.content);
与
应该返回phantomjs切换到的框架的内容
如果iframe来自另一个域,则可能需要添加--web security=no这样的选项:
phantomjs --web-security=no myscript.js
作为补充信息,xMythicx所说的可能是真的。一些iFrame在页面加载完成后通过Javascript呈现。如果iframe内容为空,则需要等待所有资源完成加载,然后才能开始从页面获取内容。但这是另一个问题,如果你需要答案,我建议你问一个新问题,我会在那里回答。对于iFrame和 phantomjs——web安全=否
对我的情况有帮助:1。切换前和切换后是否尝试调用
page.render()
,以查看生成了哪些图像?2.您是否确信加载的URL中确实存在名称框架?
phantomjs --web-security=no myscript.js