Cannot navigate with casperjs evaluate and __doPos

2019-04-10 17:34发布

问题:

When I try to navigate the pagination on sites with links where href is a __doPostBack function call, I never achieve the page change.

I am not sure what I am missing, so after a few hours of messing around I decided to see if someone here can give me a clue. This is my code (uber-simplified to show the use case).

var casper = require('casper').create({
    verbose: true,
    logLevel: "debug"
});
casper.start('http://www.gallito.com.uy/inmuebles/venta');
// here i simulate the click on a  link in the pagination list
casper.evaluate(function() {
    __doPostBack('RptPagerDos$ctl08$lnkPage2','');
});
casper.then(function() {
    console.log(' new location is ' + this.getCurrentUrl());
    var i=casper.evaluate(function(){
        return $(".aspNetDisabled").text();
    });
    console.log(i);
});
casper.run();

I tried with casper's click() and a simple jQuery click on evaluate, but that does not work because the href is a call to the __doPostBack function.

I am using casperjs 1.1.0-beta3 and phantomjs 1.9.7. I checked for similar issues and I saw this post CasperJS : how to call __doPostBack but the solution there does not work for me, and apparently it did not work for the OP either.

Thanks in advance. Please let me know if you need any more details.

回答1:

I was able to navigate the pagination by changing

casper.evaluate(function() {
    __doPostBack('RptPagerDos$ctl08$lnkPage2','');
});

To this:

casper.then(
    function(){
        casper.evaluate(   function() {
            var insertHTML='<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /><input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" /><input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />';
            $("#Form1 .aspNetHidden").html(insertHTML);

            $("#Form1 .aspNetHidden #__EVENTTARGET").val('RptPagerDos$ctl04$lnkPage2');


            $("#Form1").submit();

        });
    }
);

I noticed that even trying to submit the form directly was a problem, it looks like for some reason, it was not finding the elements it needed (i tried casper's fill() function and got crashes because the form inputs were not present)