How to change current page in Phantomjs using butt

2019-03-01 08:28发布

问题:

This question already has an answer here:

  • Open link in PhantomJS synchronously 1 answer

I have start to using Phantomjs and I don't understand something. How to change current page using buttons? For example I've got a code:

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

page.open('https://ru-ru.facebook.com', function() {
    page.injectJs('jQuery.js');
        page.evaluate(function() {
        $('#email').val('MyLogin');
        $('#pass').val('MyPass');
        $('#u_0_l').click();
        });
    page.render('example.png');
    phantom.exit();
});

So after clicking a button I need to go to the next page. How can I do this?

回答1:

Assuming the $.click() actually worked, you need to wait until the page has loaded.

setTimeout:

You may feel lucky and try it with

page.evaluate(function() {
    // form vals and click
});
setTimeout(function(){
    page.render('example.png');
    phantom.exit();
}, 3000); // 3 sec timeout

waitFor selector:

You may use the waitFor example from the phantomjs package to wait for an element that you know is only on the loaded page, but not on the current page.

waitFor loadFinished:

You may combine waitFor with the onLoadFinished callback handler to do wait for the next page to load. This is preferable since this would work every time and would not impose overshooting with a conservative timeout.

Just use CasperJS:

Nothing more to add.