Slava Fomin II
Slava Fomin II

Reputation: 28651

Wait for multiple alternative selectors with CasperJS

I have an application, that can react differently to the actions of my CasperJS script, so I need to wait for different selectors simultaneously and act according to the first matched selector. However, it looks like CasperJS can only wait for one selector at a time.

How do I wait for different reactions and divert execution of the script to the different branches?

Upvotes: 2

Views: 545

Answers (1)

istr
istr

Reputation: 171

You might want to try to wait for the combined selectors (CSS comma-separated lists act as a logical or) and then check for each selector if it exists in the page and branch, along the lines of this (untested) pseudo-code:

var selectors = [
  '.my.first .fancy .selector',
  '.boring'
];
casper.waitForSelector(selectors.join(', '), function(){
  var idx;
  for (idx = 0; idx < selectors.length; ++idx) {
    if (casper.exists(selectors[idx])) {
      doSomething(idx);
    }
  }
});

Upvotes: 4

Related Questions