Phantomjs-如何填充表单,提交并获取结果? [英] Phantomjs - How to populate a form, submit and get the results?

查看:83
本文介绍了Phantomjs-如何填充表单,提交并获取结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法进行简单的表单提交.

I can't seem to do a simple form submit.

下面是我将测试"提交到Google搜索表单并打印出结果的代码.

Below is the code that I did to submit "Test" to the Google search form and print out the results.

var url = 'http://www.google.com/',
    page = new WebPage();

page.open(url, function(status) {
    if (status !== 'success')
    {
      console.log('Unable to access network');
      phantom.exit();
      return;
    }
    else
    {
        page.includeJs("//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js", function() {
            page.evaluate(function() {
                $('#gbqfq').val("Test");

                $("#gbqfba").click();

            });

            page.render('google.png');
            phantom.exit();
        });
    }
});

任何人都可以告诉我该怎么做?我在这里和其他站点四处张望,但似乎无济于事.

Anyone can show me how to do this? I have looked around here and in other sites but nothing seemed to work.

推荐答案

仅在单击"后立即呈现页面是不够的.您必须给Web引擎足够的时间进行所需的调用并执行生成的JavaScript.

It isn't sufficient to render a page immediately after "clicking". You have to give the web engine time to make whatever calls are required and execute the resulting JavaScript.

致电评估后,请考虑以下内容:

Consider the following after your call to evaluate:

window.setTimeout(
  function () {
    page.render( 'google.png' );
    phantom.exit(0);
  },
  5000 // wait 5,000ms (5s)
);

顺便说一下,点击可能有效也可能无效,这取决于它是哪种元素.如果这不起作用,建议您在Internet上搜索如何单击DIV或它碰巧是哪种类型的元素(有一种涉及创建鼠标事件的技术).

By the way - the click may or may not work depending on what kind of element it is. If that doesn't work I suggest you search the Internet for how to click on a DIV or whatever type of element it happens to be (there is a technique which involves creating a mouse event).

这篇关于Phantomjs-如何填充表单,提交并获取结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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