How to use fillSelectors in casperjs

2019-09-19 05:36发布

问题:

I need to get some content from the page, but if I use fillSelectors() the content is not load. Maybe i need use evaluate(), but i dont undestend where and how.

var casper = require('casper').create()
casper.start('http://console.matomycontentnetwork.com/', function() {
  this.fillSelectors('form#login-form', {
      'input[name="username"]':    'jpost',
      'input[name="password"]':    'matomy123'
  }, true);
  this.clickLabel("Sign In", 'button');
});

casper.then(function() {
  var start_date = '09/01/2015';
  var end_date = '10/07/2015';
  this.evaluate(function() {
    $('#report-range').val('custom').change();
  });

  this.fillSelectors('form#report-form', {          
    'input[name="report[start_date]"]': start_date,
    'input[name="report[end_date]"]':  end_date,
    'input[id="grouping-option-3"]' : true,
    'input[id="grouping-option-2"]' : true,
    'input[id="grouping-option-4"]' : true
  }, true);

  this.click("#run-report");
  this.wait(10000, function () {
    this.echo('.geo_country_name.innerText: ' + this.evaluate(function() {
      return document.querySelector('.geo_country_name').innerText;
    }));
    this.echo('td.alignright:nth-child(5).innerText: ' + this.evaluate(function() {
      return document.querySelector('td.alignright:nth-child(5)').innerText;
    }));
  });
});

casper.run();

Can you help with that?

回答1:

Here are some guesses:

If you want to click on some button to submit a form, don't automatically submit a form. Remove the last argument:

this.fillSelectors('form#report-form', {          
    'input[name="report[start_date]"]': start_date,
    'input[name="report[end_date]"]':  end_date,
    'input[id="grouping-option-3"]' : true,
    'input[id="grouping-option-2"]' : true,
    'input[id="grouping-option-4"]' : true
});

PhantomJS doesn't support element.innerText. You need to use element.textContent.