Phantomjs - How to populate a form, submit and get the results?

早过忘川 提交于 2019-12-04 17:47:19

问题


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

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.


回答1:


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)
);

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).



来源:https://stackoverflow.com/questions/16687054/phantomjs-how-to-populate-a-form-submit-and-get-the-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!