2

I'm using CasperJS to automate some web stuff. I want to select all the li in a list and pick a specific li from that list based off information contained within it. I then want to click on a link within the selected li. After getting to the page I want, I have the following code:

casper.then(function() {
  var nodes = this.evaluate(function(){
    var li_nodes = document.querySelectorAll('ul#merchants li');
    return Array.prototype.map.call(lis, function(e) {
      return e;
    });
  });
});

That's not giving me back an array of nodes though. If instead I have:

return e.innerText

I get text back, but that doesn't help me much.

ukejoe
  • 737
  • 2
  • 9
  • 20
  • you could always take that text an use an xpath to have capser click the link which contains the text you extracted, should certain criteria be met. – Chris Hawkes Mar 10 '14 at 13:26

1 Answers1

0

You can't pass back DOM elements from evaluate () : casperjs: evaluating document.querySelector returns a null and http://casperjs.readthedocs.org/en/latest/modules/casper.html#getelementinfo Only return primitive type : object, array, boolean,string .... What you could do is :

this.click("ul#merchants li:nth-child(3)");

See css selectors for child, etc... : http://www.w3schools.com/cssref/css_selectors.asp

Or like Chris said, using XPath selectors :

var x = require('casper').selectXPath;
this.click(x("//ul[@id='merchants']//li[3]"));

filtering with some text content :

this.click(x("//ul[@id='merchants'][contains(text(),'someText')]//li[3]"));

We don't have "contains text" in css3 selectors, so when i want to do it with css selectors, i do it with jQuery ( :contains() ). (you need to inject jQuery clientside if necessary) or use this.clickLabel("text");

Community
  • 1
  • 1
Fanch
  • 3,274
  • 3
  • 20
  • 51