1

there are some button share the same id or class that casperjs cant classify, i want to click on the second or N selector match, i can do it with:

document.querySelectorAll('[id="something"]').click();

but i have to do it with the following basic format:

casper.then(function (){
    this.click(id="something");
});

or. is there anyway i can make the following statement work?

var clickthis= '[id="something"]';
document.querySelectorAll(clickthis).click();

the var clickthis is working in casper.click, but cant work in document.querySelector

thanks!

Robert Choy
  • 105
  • 1
  • 8

2 Answers2

2

Try this way:

casper.evaluate(function(sel) {
    document.querySelectorAll(sel)[1].click();
}, '[id="something"]');
jackchen
  • 71
  • 8
0

To click on a second element, you can use:

function click(sel,n){var event=document.createEvent('MouseEvents');
event.initMouseEvent('click',1,1,window,1,0,0,0,0,0,0,0,0,0,null);
document.querySelectorAll(sel)[n].dispatchEvent(event);}

var casper = require('casper').create();
casper.start('http://domu-test-2/',function(){
this.evaluate(function(click){
click('li.node-readmore a',1);// click on a second element of the array.
},click);
})
.wait(3000,function(){this.capture('test.png');})
.run();