How to send arrow keys using sendKeys()?

2,724 views
Skip to first unread message

Victor Hooi

unread,
Jan 1, 2014, 10:01:21 PM1/1/14
to casp...@googlegroups.com
Hi,

This might be a basic question - but how exactly do you send arrow keys under CasperJS?

I have a <input> auto-complete field - I need to send a search string, then press the "Down" key, followed by "Enter" to select that result (or I could mouse-down on the result, I suppose).

The CasperJS docs for sendKeys() only specifically mention sending alpha-numeric characters, or using modifiers - they don't mention things like arrow keys:


Is there an easy way to send arrow keys under CasperJS?

Cheers,
Victor

Jeremy G.

unread,
Jan 29, 2014, 5:42:36 PM1/29/14
to casp...@googlegroups.com

// down arrow = 40
// left arrow     = 37
// right arrow   = 39
// up arrow      = 38

casper.thenEvaluate( function(){
    var e = jQuery.Event("keydown");
    e.which = 40; // down arrow
    $("input").trigger(e);
});

This page makes it really easy to find any keycode you want.
http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

nuzz

unread,
Feb 25, 2014, 3:19:21 PM2/25/14
to casp...@googlegroups.com
This was super helpful, I modified things to work for my situation - backbonejs app with input fields that don't necessarily have a form - inputs that respond to keypress ENTER

casper.thenEvaluate(function() {
var e = jQuery.Event("keypress");
e.which = 13;
e.keyCode = 13;
$("[data-widget=SomeFilter][data-label=FOO] input").trigger(e);
});
Reply all
Reply to author
Forward
0 new messages