如何使用 PhantomJS 的 sendEvent 函数在 CasperJS 中键入数字或小写字母
How to type number or lower letter in CasperJS using PhantomJS' sendEvent function
我确定之前有人问过,我只是花了几个小时寻找答案,但大多数人都在 CasperJS 中输入 'enter' 键。
我可以输入 "A" - "Z",但对 "a"-"z" 和“1”-“9”没有任何反应。
这是我的尝试:
casper.then(function (){
this.page.sendEvent("keypress", this.page.event.key.F);
this.page.sendEvent("keypress", this.page.event.key.c);
this.page.sendEvent("keypress", this.page.event.key.4);
console.log('there is only F.. but no c and 4');
});
你看不到其他的,因为 PhantomJS 仅使用大写字符串 (F
) 来定义 page.event.key.F
(source) 中的字母 f
。
casper.page.sendEvent("keypress", casper.page.event.key.F);
应该给你一个小写的F:f
。如果你想以这种方式输入大写字符,那么你需要传入修饰符 (documentation)
casper.page.sendEvent('keypress', casper.page.event.key.F, null, null, 0x02000000);
JavaScript 中的变量不能以数字开头,因此 this.page.event.key.4
不能 用于访问以 a 开头的 属性数字。如果要键入 4
,则需要使用字符串语法:this.page.event.key["4"]
.
这很麻烦。为什么不直接使用字符串来输入这些字符?
casper.page.sendEvent("keypress", "Fc4");
我确定之前有人问过,我只是花了几个小时寻找答案,但大多数人都在 CasperJS 中输入 'enter' 键。
我可以输入 "A" - "Z",但对 "a"-"z" 和“1”-“9”没有任何反应。 这是我的尝试:
casper.then(function (){
this.page.sendEvent("keypress", this.page.event.key.F);
this.page.sendEvent("keypress", this.page.event.key.c);
this.page.sendEvent("keypress", this.page.event.key.4);
console.log('there is only F.. but no c and 4');
});
你看不到其他的,因为 PhantomJS 仅使用大写字符串 (F
) 来定义 page.event.key.F
(source) 中的字母 f
。
casper.page.sendEvent("keypress", casper.page.event.key.F);
应该给你一个小写的F:f
。如果你想以这种方式输入大写字符,那么你需要传入修饰符 (documentation)
casper.page.sendEvent('keypress', casper.page.event.key.F, null, null, 0x02000000);
JavaScript 中的变量不能以数字开头,因此 this.page.event.key.4
不能 用于访问以 a 开头的 属性数字。如果要键入 4
,则需要使用字符串语法:this.page.event.key["4"]
.
这很麻烦。为什么不直接使用字符串来输入这些字符?
casper.page.sendEvent("keypress", "Fc4");