node-phantom handle hyperlink of iframe fail

倖福魔咒の 提交于 2019-12-25 03:48:16

问题


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?

example:

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();

      });

回答1:


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.

You would need to use page.switchToFrame to switch to the frame to execute stuff on the frame without copying it first.

It looks like switchToFrame is not implemented in phantomjs-node. You could try node-phantom.

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()

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){...});


来源:https://stackoverflow.com/questions/25255512/node-phantom-handle-hyperlink-of-iframe-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!