无法使用phantomJS获取iframe的元素
我正在寻找一种方法来控制文档中的元素。 问题是,该文件被附加到一个iFrame。
这是一个例子:
.goto('https://wirecard-sandbox-engine.thesolution.com/engine/hpp/') .use(iframe.withFrameName('wc', function (nightmare) { nightmare .type('#account_number', accountNumber) .screenshot(resultfolder + '\\06-Card-Number.png') .type('#card_security_code', testData.securityCode) .selectIndex('#expiration_month_list', 1) .selectIndex('#expiration_year_list', 4) .click('span[id="hpp-form-submit"]') }))
我在上面要做的是:
- 获取与url的iFrame。
- 使用这个来获得对iFrame中每个元素的控制。
我遇到了类似的问题。 由于某种原因(Windows?已过时?), nightmare-iframe
-frame包不适合我。
所以我用纯DOM做了这个,它会转录给你:
var info = { "account": account_number, "security": testData.security_code; }; nightmare.wait( () => { /* Return true when the iframe is loaded */ var iframe = document.querySelector("iframe[name='wc']"); if (!iframe) { return false; } var iframeDocument = iframe.contentDocument || iframe.contentWindow.document; return iframeDocument.querySelector("#account_number") != null; }).evaluate( (info) => { var iframe = document.querySelector("iframe[name='wc']"); var iframeDocument = iframe.contentDocument || iframe.contentWindow.document; iframeDocument.querySelector("#account_number").value = info.account; iframeDocument.querySelector("#card_security_code").value = info.security; //also use DOM to set proper date ... iframeDocument.querySelector("span#hpp-form-submit").click(); }, info).then( () => { console.log("submit done!"); }).catch((error) => { console.error("Error in iframe magic", error); });
当然,如果iframe与主页面在同一个域中并且iframe允许相同的源,这当然也是可以的。 我的用例是从一个商人的页面login到贝宝。
对于不同来源nightmare-iframe
, nightmare-iframe
将是使用的软件包,但是需要一个错误才能看到问题所在。