0

I am trying to use CasperJS to populate two fields, submit a form, and print out the content of the following page. However, it does not appear that I am successfully clicking the button to go to the next page. I attempted to follow the tutorials/examples on CasperJS and answers on SO like How to login into a website with CasperJS?. However, the difference is that I am not submitting a but rather a table with several filled out values.

Please find my code below:

var casper = require('casper').create();

casper.start('https://a836-propertyportal.nyc.gov/Default.aspx', function() {});


casper.evaluate(function(block, lot) {
    document.querySelector('#ctl00_SampleContent_ctl01_txtBlock').value = block;
    document.querySelector('#ctl00_SampleContent_ctl01_txtLot').value = lot;
    document.querySelector('#ctl00_SampleContent_ctl01_btnSearchBBL').click();
}, '01000', '0011');


casper.run(function() {
    // echo results in some pretty fashion
    this.echo('we are this far in the code..');
    // this.echo(this.getPageContent());
    var links = document.querySelectorAll('td.contentDataElement');
    this.echo(links.length);
    for(var i=0; i<links.length; i++) {
        this.echo(links[i]);
    }
});
Community
  • 1
  • 1
NumenorForLife
  • 1,736
  • 8
  • 27
  • 55

1 Answers1

2

casper.evaluate is not a step function, but you use it on the same level as start and run. So change casper.evaluate to casper.thenEvaluate.

Also you don't have access to document in casper, but only inside the page context like you did with the casper.evaluate call.

Your fixed code uses the casper.getElementsInfo function to get some attributes of your desired elements. You cannot pass DOM elements from the page context to casper, they have to be of type number, string, [] or {}.

var casper = require('casper').create();

casper.start('https://a836-propertyportal.nyc.gov/Default.aspx');

casper.thenEvaluate(function(block, lot) {
    document.querySelector('#ctl00_SampleContent_ctl01_txtBlock').value = block;
    document.querySelector('#ctl00_SampleContent_ctl01_txtLot').value = lot;
    document.querySelector('#ctl00_SampleContent_ctl01_btnSearchBBL').click();
}, '01000', '0011');

casper.then(function() {
    // echo results in some pretty fashion
    this.echo('we are this far in the code..');
    // this.echo(this.getPageContent());
    var links = this.getElementsInfo('td.contentDataElement');
    this.echo(links.length);
    for(var i=0; i<links.length; i++) {
        this.echo(JSON.stringify(links[i]));
    }
});

casper.run();
Artjom B.
  • 61,146
  • 24
  • 125
  • 222