1

my webpage has a listener to the enter key press event. I am trying to run the casperjs code below to trigger this event, but without success.

Although no error is prompted out, the (evaluate) function returns true and the code works fine from my chrome console, the function result, that should be sending a request to the server is never happening

casper.then(function(){
    var result = this.evaluate(function(term){
        var search_form_id = "#search-form";
        $(search_form_id).val(term);

        jQuery(search_form_id).trigger(jQuery.Event('keypress', {which: 13, keyCode: 13}));

        return true;
    }, 'Techcrunch');
    console.log(result);
});

Is that any issue regarding PhantomJS and jQuery events?

marcelosalloum
  • 3,481
  • 4
  • 39
  • 63
  • 1
    I tried something similar (with 'enter' keypress, 'Down' too) without success. It might be possible it's a phantom issue. – Fanch May 12 '14 at 12:27

1 Answers1

9

It looks like, you can't trigger the keypress event using jQuery. There is a workaround using the underlying casper.page.sendEvent function. Though it is necessary to focus on the element, where the keypress will be triggered. In the following example I use the keepFocus option of the sendKeys function.

var casper = require('casper').create();
casper.start("https://duckduckgo.com/");
casper.then(function() {
    this.sendKeys("#search_form_homepage input[name=q]", "casperjs", { keepFocus: true });
    this.capture("typed.png");
    this.page.sendEvent("keypress", this.page.event.key.Enter);
});

casper.waitForSelector("#links_wrapper");

casper.then(function() {
    this.capture("searched.png");
});

casper.run();
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • If you have problems using https or sites that use https resources, see this question [PhantomJS failing to open HTTPS site](http://stackoverflow.com/questions/12021578/phantomjs-failing-to-open-https-site) and especially [this answer](http://stackoverflow.com/a/26417660/1816580). – Artjom B. Oct 28 '14 at 18:17
  • B, in my case the above solution is not working. Below is my code casper.then(function() { this.sendKeys("#command-prompt", "casperjs", { keepFocus: true }); this.capture("typed.png"); this.page.sendEvent("keypress", this.page.event.key.Enter); }); – Prateek.Naik Aug 27 '15 at 06:40
  • @Prateek.Naik Have you registered to the various error events? – Artjom B. Aug 27 '15 at 08:24
  • Yes... i have registered – Prateek.Naik Aug 27 '15 at 09:00
  • I'm afraid I can't help you there. You can ask a new detailed question. You should include a runnable example. – Artjom B. Aug 27 '15 at 09:06
  • this answer is accepted. But @Prateek.Naik,... have u figured it out? – gumuruh Jul 18 '16 at 06:17
  • @gumuruh, nope buddy still haven't found any solution for that the code which itried is this one:```casper.then(function() { this.sendKeys("#command-prompt", "casperjs", { keepFocus: true }); this.capture("typed.png"); this.page.sendEvent("keypress", this.page.event.key.Enter); });``` – Prateek.Naik Jul 18 '16 at 06:30
  • would it be possible for that event to be triggered inside the evaluate() functions call ? @Prateek.Naik – gumuruh Jul 18 '16 at 06:40