提交简单表格并在屏幕之前和之后捕获 [英] submitting simple form and capturing before and after screen

查看:80
本文介绍了提交简单表格并在屏幕之前和之后捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用phantomjs提交简单表单

im trying to submit a simple form with phantomjs

这是我的代码

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

page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
page.onLoadFinished = function(){

    page.render("after_post.png");
    console.log("done2!" );
    phantom.exit();

};

page.open('http://localhost/bimeh/phantom/testhtml.php', function(status) {

    page.includeJs("http://code.jquery.com/jquery-latest.min.js", function() {

        page.evaluate(function() {
           $("[name='xxx']").val('okk');
            page.render("pre_post.png");
            console.log('done1!');
            $('#subbtn').click();
        });
    });
});

问题是我没有得到pre_post.png图片,这是我的输出

the problem is i dont get the pre_post.png image her eis my output

$ phantomjs test.js
done2!

似乎onLoadFinished在page.evaluate可以做任何事情之前被调用...也在after_post.png中,我在提交操作之前获取了表格图片

it seems onLoadFinished is called before page.evaluate can do anything ... also in the after_post.png i get picture of form before submit action

我使用phantomjs 1.98(我已从2.1降级,因为它显然由于qt中的某些错误而希望输出错误)

im using phantomjs 1.98 (i've downgraded from 2.1 becuz it want outputting errors apparently due to some bug in qt )

推荐答案

这是错误的:

page.evaluate(function() {
    page.render("pre_post.png"); // <----
});

page.evaluate就像您在浏览器中加载了页面,然后在开发人员工具控制台中运行脚本一样.那里没有变量page. page属于PhantomJS级别的脚本:

page.evaluate is as if you loaded a page in a browser and then run scripts in developer tools console. There is no variable page in there. page belongs to a PhantomJS level script:

page.open('http://localhost/bimeh/phantom/testhtml.php', function(status) {

    page.includeJs("http://code.jquery.com/jquery-latest.min.js", function() {
        page.render("pre_post.png");

        page.evaluate(function() {
            $("[name='xxx']").val('okk');
            $('#subbtn').click();
        });
    });
});

每次页面加载完成时都会调用

page.onLoadFinished:第一次PhantomJS打开脚本,第二次提交表单时.您可以保持其功能不变,在这种情况下,如果提交表单,则原始页面的第一个屏幕截图将被第二个屏幕截图覆盖.

page.onLoadFinished is called every time a page has finished loading: the first time PhantomJS opens the script and the second when form is submitted. You may keep your function as it is and in this case if form is submitted the first screenshot of original page will be overwritten with the second screenshot.

但是由于PhantomJS中的按钮没有click方法,您的表单最有可能不会提交,它是在2.x中添加的.

However most likely your form won't be submitted because buttons don't have a click method in PhantomJS, it was added in 2.x.

您的脚本还缺少一个关键的东西:错误控制.请使用page.onError回调来捕获页面上的所有错误(您可以从此处简单地复制该函数: http://phantomjs.org/api/webpage/handler/on-error.html )

Your script also lacks a crusial thing: error control. Please use page.onError callback to catch any errors on the page (you may simply copy the function from here: http://phantomjs.org/api/webpage/handler/on-error.html )

这篇关于提交简单表格并在屏幕之前和之后捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆