node-phantom处理iframe的超链接失败 [英] node-phantom handle hyperlink of iframe fail
问题描述
最近,我通过 phantomjs-node 集成了node和phantomjs。我打开了有iframe元素的页面,我可以获取iframe的超链接元素,但是当我执行点击它时失败了。
你有办法吗?有人可以帮帮我吗?
Recently, i integrate node and phantomjs by phantomjs-node. I opened page that has iframe element, i can get the hyperlink element of iframe, but failed when i execute click on it. Do you have a way? Anyone can help me?
例如:
page.open(url);
...
page.evaluate(function(res){
var childDoc = $(window.frames["iframe"].document),
submit = childDoc.find("[id='btnSave']"),
cf = submit.text();//succeed return text
submit.click()//failed
return cf;
},function(res){
console.log("result="+res);//result=submit
spage.render("test.png");//no submit the form
ph.exit();
});
推荐答案
您无法在iframe中执行内容。你只能读它。您甚至从iframe创建了一个新文档,该文档仅包含iframe的文本表示形式,但它与原始iframe没有任何关联。
You can't execute stuff in an iframe. You can only read from it. You even created a new document from the iframe, which will only contain the textual representation of the iframe, but it is in no way linked to the original iframe.
你会需要使用 page.switchToFrame
切换到框架以执行在框架上而不先复制它。
You would need to use page.switchToFrame
to switch to the frame to execute stuff on the frame without copying it first.
它看起来像 switchToFrame
未在phantomjs-node中实现。您可以尝试 node-phantom 。
It looks like switchToFrame
is not implemented in phantomjs-node. You could try node-phantom.
如果iframe位于同一个域中,您可以从此处尝试以下操作:
If the iframe is on the same domain you can try the following from here:
submit = $("iframe").contents().find("[id='btnSave']")
cf = submit.text();
submit.click()
如果iframe不是来自同一个域,你将会需要创建关闭Web安全性的页面:
If the iframe is not from the same domain, you will need to create the page with web security turned off:
phantom.create('--web-security=false', function(page){...});
这篇关于node-phantom处理iframe的超链接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!