0

Is it possible to click page element in casper js without passing selector? I mean I can't do that:

casperjs.thenClick('#test');

But I have

var testV = document.querySelector('#test');

And I want do something like this:

casperjs.thenClick(testV);

For now it doesn't work

Piotr Wu
  • 1,362
  • 3
  • 14
  • 31

1 Answers1

1

You are using thenClick improperly. Make sure the then.click is not contained within a casper.evaluate block and note there is not the js on the end of casper. It should be implemented as:

casper.thenClick('a', function() {
    this.echo("I clicked on first link found, the page is now loaded.");
});

If you want to just perform a regular click on selector you can do the following:

casper.then(function() {
    // Click on 1st result link
    this.click('h3.r a');
});

If you would like to use javascript, make sure you are within a casper.evaluate statement. You can use the following:

casper.then(function() {
    casper.evaluate(function() {
        var testV = document.getElementById("test");
        testV.click();
    });
});
Christopher Ellis
  • 1,106
  • 1
  • 8
  • 12
  • ok 'casperjs' was mistake only when I type that here. In my code it was ok :) But the rest still doesn't work. – Piotr Wu Nov 27 '13 at 15:50
  • Please mark my answer as a correct answer and I am reviewing your code from question: [Here](http://stackoverflow.com/q/20243033/2812723) – Christopher Ellis Nov 27 '13 at 15:55
  • I've tested it and first doesn't work. But I test this on other site and it work. It work on input submit, but it doesn't on anchor. So your solution is good, but not perfect :) any idea what with anchors ? – Piotr Wu Nov 27 '13 at 17:23