1

I'm trying to learn CasperJS and for a start I would like to create small yummly.com scraper. I want to scrape a list of 100-1000 popular recipes.

Everything was going great till I tried to click on the element that triggers more recipes to load. I've tried every way, xpath, selector, label. I really don't know what could be the problem.

Here is my code:

var utils = require('utils');

var casper = require('casper').create({
    verbose: true,
    logLevel: 'error',
    pageSettings: {
        loadImages: false,
        loadPlugins: false,
        userAgent: 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36'
    }
});

var url = "http://www.yummly.com/browse/popular-now";

var x = require('casper').selectXPath;

function getLinks() {
    var links = document.querySelectorAll('div.y-title a');
    return Array.prototype.map.call(links, function(e) {
        return e.getAttribute('href');
    });
}

casper.start(url, function() {
    console.log(this.getTitle());
});

// Try to click with xpath
casper.wait(2000, function() {
    casper.click(x('//*[@id="more"]'));
});

// Try to click with label
casper.wait(2000, function() {
    this.clickLabel('See more', 'a');
});

// Try to click with selector
casper.wait(2000, function() {
    casper.click('#more');
});

// Try to wait for selector
casper.waitForSelector('#more', function() {
      this.click('#more');
      casper.wait(2000, function(){
        links = this.evaluate(getLinks);
        console.log(links);
    });
});

casper.run();

I really don't know what could cause this problem.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Please register to the `resource.error`, `page.error`, `remote.message` and `casper.page.onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf)). Maybe there are errors. – Artjom B. Apr 17 '15 at 09:31
  • Unfortunately everything seems the to be ok with these tests. It seems to me like to problem could be with invoking javascript in href. Maybe CasperJS is not loading the Javascript correctly. I really don't know :( – TopperHarley Apr 18 '15 at 11:16
  • http://stackoverflow.com/questions/29616350/casperjs-click-not-firing-click-event – Ciro Santilli OurBigBook.com Nov 16 '15 at 16:49

1 Answers1

0

Your code works fine for PhantomJS 2.

The click doesn't work for PhantomJS 1.x. I tried the three different variants of clicking from this question with PhantomJS versions 1.9.7 and 1.9.8:

  • using JavaScript: el.dispatchEvent(ev)
  • using PhantomJS: casper.page.sendEvent('click', ...)
  • using jQuery: $(el).click()

None of them worked. So you will need to update your version.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • [CasperJS 1.1.0 docs](http://docs.casperjs.org/en/latest/installation.html) say it requires "PhantomJS 1.8.2 or greater, but less than 2.0." – Ciro Santilli OurBigBook.com Nov 16 '15 at 16:50
  • @CiroSantilli Yes, but you can [install CasperJS from git](http://docs.casperjs.org/en/latest/installation.html#installing-from-git) which works fine with PhantomJS 2.0.0. – Artjom B. Nov 16 '15 at 16:55