我正在尝试使用phantomJS(这是一个很棒的工具btw!)为我有登录凭据的页面提交表单,然后将目标页面的内容输出到stdout.我能够使用幻像访问表单并成功设置其值,但我不太确定提交表单和输出后续页面内容的正确语法.到目前为止我所拥有的是:
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="mylogin@somedomain.com";
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 …Run Code Online (Sandbox Code Playgroud) 如何单击PhantomJS中的元素?
page.evaluate(function() {
document.getElementById('idButtonSpan').click();
});
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误"未定义不是一个函数......"
如果我改为
return document.getElementById('idButtonSpan');
Run Code Online (Sandbox Code Playgroud)
然后打印出来,
然后它打印[object object],因此元素确实存在.
该元素充当按钮,但它实际上只是一个span元素,而不是提交输入.
我能够点击这个按钮点击Casper,但Casper有其他限制,所以我回到了PhantomJS.