发送keydown,在nightmare.js中键入按键

在nightmare.js中的types方法将文本分配给input控件的值属性。 由于这个实现keydown,keypress事件不会触发你正在试图抓取的页面上。 任何方式发送'types'后的keydown事件?

编辑1-

这是一个使用jQuery发送事件的例子,

var Nightmare = require('nightmare'); var vo = require('vo'); var nightmare = Nightmare(); vo(run)(function(err, result) { if (err) throw err; }); function *run() { var title = yield nightmare .goto('http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes') .type('#txtChar','\n') .evaluate(function() { var e = $.Event( "keypress", { which: 13 } ); $('#txtChar').trigger(e); return "Key you pressed is : " + $('#txtChar').val(); }); yield nightmare.pdf('type.pdf'); setTimeout(function () { console.log( ' ' + title); nightmare.end(); process.exit(); },2000); } 

编辑2-

使用unicode等效的键作为参数types的方法以某种方式调用附加的事件,不完全确定这个黑客工作,但它的工作原理!

这里是工作的例子 –

 var Nightmare = require('nightmare'); var vo = require('vo'); var nightmare = Nightmare(); vo(run)(function(err, result) { if (err) throw err; }); function *run() { var title = yield nightmare .goto('http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes') .type('#txtChar','1') //so we have focus on textbox .type('document', '\u000d') .evaluate(function() { return "Key you pressed is : " + $('#txtChar').val(); }); yield nightmare.pdf('type.pdf'); setTimeout(function () { console.log( ' ' + title); nightmare.end(); process.exit(); },2000); } 

你可以评估一个页面,并使用纯JS或jQuery发送一个keydown事件,jQuery是简单的方法,但它最注入。

使用jQuery:

 .inject('js', '/jquery-2.1.4.min.js') .evaluate(function () { var e = $.Event( "keypress", { which: 13 } ); $('#yourInput').trigger(e); }); 

编辑:看起来像是在梦魇中支持关键事件。 看看https://github.com/segmentio/nightmare/issues/244和https://github.com/segmentio/nightmare/issues/147

EDIT2:不,应该是.type('document', '\u000d') 。 得到了错误的Unicode字符。