如何使用 PhantomJS 提交表单
我正在尝试使用 phantomJS(顺便说一句,这是一个多么棒的工具啊!)为我拥有登录凭据的页面提交表单,然后将目标页面的内容输出到标准输出。我可以使用 phantom 成功访问表单并设置其值,但我不太确定提交表单并输出后续页面内容的正确语法是什么。到目前为止我所拥有的是:
var page = new WebPage();
var url = phantom.args[0];
page.open(url, function (status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
console.log(page.evaluate(function () {
var arr = document.getElementsByClassName("login-form");
var i;
for (i=0; i < arr.length; i++) {
if (arr[i].getAttribute('method') == "POST") {
arr[i].elements["email"].value="[email protected]";
arr[i].elements["password"].value="mypassword";
// This part doesn't seem to work. It returns the content
// of the current page, not the content of the page after
// the submit has been executed. Am I correctly instrumenting
// the submit in Phantom?
arr[i].submit();
return document.querySelectorAll('html')[0].outerHTML;
}
}
return "failed :-(";
}));
}
phantom.exit();
}
I'm trying to use phantomJS (what an awesome tool btw!) to submit a form for a page that I have login credentials for, and then output the content of the destination page to stdout. I'm able to access the form and set its values successfully using phantom, but I'm not quite sure what the right syntax is to submit the form and output the content of the subsequent page. What I have so far is:
var page = new WebPage();
var url = phantom.args[0];
page.open(url, function (status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
console.log(page.evaluate(function () {
var arr = document.getElementsByClassName("login-form");
var i;
for (i=0; i < arr.length; i++) {
if (arr[i].getAttribute('method') == "POST") {
arr[i].elements["email"].value="[email protected]";
arr[i].elements["password"].value="mypassword";
// This part doesn't seem to work. It returns the content
// of the current page, not the content of the page after
// the submit has been executed. Am I correctly instrumenting
// the submit in Phantom?
arr[i].submit();
return document.querySelectorAll('html')[0].outerHTML;
}
}
return "failed :-(";
}));
}
phantom.exit();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想通了。基本上这是一个异步问题。您不能只是提交并期望立即呈现后续页面。您必须等到下一页的 onLoad 事件被触发。我的代码如下:
I figured it out. Basically it's an async issue. You can't just submit and expect to render the subsequent page immediately. You have to wait until the onLoad event for the next page is triggered. My code is below:
此外,CasperJS 为 PhantomJS 中的导航提供了一个很好的高级界面,包括单击链接和填写表单。
CasperJS
已更新以添加 2015 年 7 月 28 日文章比较 PhantomJS 和 CasperJS。
(感谢评论者M先生!)
Also, CasperJS provides a nice high-level interface for navigation in PhantomJS, including clicking on links and filling out forms.
CasperJS
Updated to add July 28, 2015 article comparing PhantomJS and CasperJS.
(Thanks to commenter Mr. M!)
发送原始 POST 请求有时会更方便。下面你可以看到来自 PhantomJS 的 post.js 原始示例
Sending raw POST requests can be sometimes more convenient. Below you can see post.js original example from PhantomJS
正如上面提到的 CasperJS 是填写和发送表单的最佳工具。
如何填充和填充的最简单的示例使用 fill() 函数提交表单:
As it was mentioned above CasperJS is the best tool to fill and send forms.
Simplest possible example of how to fill & submit form using fill() function: