使用PhantomJS提交表单后如何获得结果?

使用PhantomJS提交表单后如何获得结果?,phantomjs,Phantomjs,我正在尝试使用PhantomJS从一个简单的表单中获取结果。我正在使用jQuery,但不起作用。我有这个HTML: 幻影! 你的名字是 下面是Javascript代码: var page=require('webpage').create(); 第页打开('http://localhost/phantom/,函数(){ 第页,包括https://code.jquery.com/jquery-3.1.1.slim.js“,函数(){ page.evaluate(函数(){ $('nombre'

我正在尝试使用PhantomJS从一个简单的表单中获取结果。我正在使用jQuery,但不起作用。我有这个HTML:


幻影!
你的名字是
下面是Javascript代码:

var page=require('webpage').create();
第页打开('http://localhost/phantom/,函数(){
第页,包括https://code.jquery.com/jquery-3.1.1.slim.js“,函数(){
page.evaluate(函数(){
$('nombre').val('Fabian');
document.forms[0]。提交();
});
page.onLoadFinished=函数(){
log($(“#nombrez”).html());
phantom.exit();
};  
});
});

页面。onLoadFinished
不能在
页面内调用。evaluate
,而是在主PhantomJS脚本内调用:

var page = require('webpage').create();

page.onLoadFinished = function(){

    var html = page.evaluate(function(){
        return document.getElementById("nombrez").innerHTML;
    });
    console.log(html);
    phantom.exit();

};  

page.open('http://localhost/phantom/', function() {
    page.includeJs("https://code.jquery.com/jquery-3.1.1.slim.js", function() {
        page.evaluate(function() {

        $('#nombre').val('Fabian');
            document.forms[0].submit();
        });
    });
});
但是,
page.onLoadFinished
在每次加载页面时都会触发,使用此实现,phantom将在第一次加载页面时退出,即使在提交表单之前也是如此


您需要实现一些检查来区分页面的第一次加载和第二次加载。例如,如果return
html
变量为空,则表示我们尚未提交页面。

非常感谢。我刚刚将它添加到您的代码中,它是否有效:if(html!=“”){console.log(html);phantom.exit();}