ragingSloth
ragingSloth

Reputation: 1104

Can't dispatch click on valid selector using CasperJS

Using Casperjs, I'm trying to dispatch a click event to one of the links on a page. It's position is dependent on the size of the window so multiple instances of it exist. For this reason I'm using a selector based on its parent. I don't dispatch the even till it's confirmed to not equal undefined and I was previously checking against null. When I print objects matching the selector I'm using I get [object Object], but when I try to dispatch the click event I receive an error stating "Cannot dispatch mousedown event on nonexistent selector". How is this possible if querySelectorAll() returns objects for the same selector. This is the code I'm using.`

casper.each(links, function() {
this.waitFor(function check() {
    return (this.evaluate(getSelector,'div.talk-hero__tools__list__actions a.rate-button') != undefined);
    },
    function then() {
        this.echo(this.evaluate(getSelector,'div.talk-hero__tools__list__actions a.rate-button'));
        this.click('div.talk-hero__tools__list__actions a.rate-button');
});
});

Upvotes: 2

Views: 397

Answers (2)

ragingSloth
ragingSloth

Reputation: 1104

So it turns out that casper.each() doesn't open the link. I solved it by adding a casper.thenOpen() around all the code in the each().

Upvotes: 1

berrberr
berrberr

Reputation: 1960

Try this (adding an anonymous function to your call to evaluate, and switch selector check from undefined to null):

this.waitFor(function check() {
  return this.evaluate(function() {
    return document.querySelector('div.talk-hero__tools__list__actions a.rate-button') !== null;
  });
}, function then() {
    this.echo(this.evaluate(getSelector,'div.talk-hero__tools__list__actions a.rate-button'));
    this.click('div.talk-hero__tools__list__actions a.rate-button');
});

Upvotes: 1

Related Questions