我在设置iframe.src
后尝试获取Iframe的文档。
我在phantom-node
打电话给我。
当iframe为空时,该函数返回error
:
fetchGuestbookEntries: function () {
var getGuestbookForPage = function (doc) {
var result = [];
var rows = doc.querySelectorAll('td[class="guestbook"]');
var date = "";
var entry = "";
for (var i = 0; i < rows.length; i++) {
date = i % 2 == 0 ? rows[i].innerText : date;
entry = i % 2 != 0 ? rows[i].innerText : entry;
if (date && entry) {
var info = date.split('\n').map(function (elem) {
return elem.replace('\n', '').trim();
});
result.push(
{
info: {
guestname: info[0],
date: new Date(info[3]).toISOString().slice(0, 10),
time: info[4].replace(/Uhr|h/gi, '').trim()
},
entry: entry.trim()
}
);
date = "";
entry = "";
}
}
return result;
};
var getPaginationSize = function () {
return document.querySelector('td[class="guestbook_navijump"]') ?
document.querySelector('td[class="guestbook_navijump"]').querySelectorAll('a').length : 0;
};
var getIframeDoc = function(ifrm) {
return ifrm.document ||
ifrm.contentDocument ||
ifrm.contentWindow.document;
};
var pagination = getPaginationSize();
var entries = [];
for (var i = 0; i <= pagination; i++) {
var paginationPageUrl = window.location.href + "?jump=" + i;
var iframe = document.createElement('iframe');
iframe.src = paginationPageUrl;
var doc = getIframeDoc(iframe);
entries.push(getGuestbookForPage(doc));
}
return entries;
}
有没有办法获取文件?
错误:
Uncaught TypeError: Cannot read property 'document' of null
at <anonymous>:2:21
at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
at Object.InjectedScript.evaluate (<anonymous>:694:21)
我通常在PhantomJS中打开页面。
答案 0 :(得分:0)
你应该能够用这个
获得它document.getElementById('myframe').contentWindow.document