如何使用硒按住非修饰键(空格键)? [英] How to press and hold non-modifier key (space key) using selenium?

查看:97
本文介绍了如何使用硒按住非修饰键(空格键)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,硒无法按住此列表中没有的键-

I met a problem, that selenium cannot press and hold key that is not in this list -

Keys.SHIFT, 
Keys.CONTROL, 
Keys.ALT, 
Keys.META,
Keys.COMMAND, 
Keys.LEFT_ALT, 
Keys.LEFT_CONTROL,
Keys.LEFT_SHIFT

仅当按住空格键时,我的应用程序才会显示说明.我想为此编写浏览器测试.

My application shows instructions only when space key is pressed and hold. I want to write browser tests for this.

我正在使用ProtractorJS,但是当您尝试将keyDown用作其他键时,硒中的任何地方似乎都对这种操作有一般性限制-这样的消息会导致异常-"Key Down/Up事件仅是有意义的用于修改键."

I am using ProtractorJS, but it seems like general limitation for such action, everywhere in selenium when you try to use keyDown for other key - you will get an exception with message like this - "Key Down / Up events only make sense for modifier keys."

这是Selenium Java代码的链接: https ://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/interactions/internal/SingleKeyAction.java#L48

here is link to Selenium Java code: https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/interactions/internal/SingleKeyAction.java#L48

并同样检查硒js代码: https://github.com/SeleniumHQ/selenium/blob/master/javascript/webdriver/actionsequence.js#L301

And same check in selenium js code: https://github.com/SeleniumHQ/selenium/blob/master/javascript/webdriver/actionsequence.js#L301

我如何按住非修饰键?就我而言,是空格键.

How i can press and hold non-modifier key? Space key in my case.

更新: 感谢Florent B.回答. 经过一点修改-对我来说完美.必须将切换添加到框架,并将事件调度到文档,而不是针对我的情况的特定元素.

UPDATE: Thanks to Florent B. answer. After little modifying - works perfectly for me. Had to add switching to frame, and dispatching event to document instead specific element for my case.

browser.switchTo().frame('workspace');  
const SIMULATE_KEY =  
"var e = new Event('keydown');" +  
"e.keyCode = 32;" +  //spacebar keycode
"e.which = e.keyCode;" +  
"e.altKey = false;" +  
"e.ctrlKey = false;" +  
"e.shiftKey = false;" +  
"e.metaKey = false;" +  
"e.bubbles = true;" +  
"document.dispatchEvent(e);";  
browser.executeScript(SIMULATE_KEY);

推荐答案

Selenium API不提供此功能.根据官方文档:

The Selenium API doesn't provide this feature. From the official documentation:

https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol

服务器必须按以下方式处理键序列: 键盘上出现的每个不需要修改键的键都作为键按下,然后按键盘上的键发送.

The server must process the key sequence as follows: Each key that appears on the keyboard without requiring modifiers are sent as a keydown followed by a key up.

不过,您可以使用一段Javascript模拟按键事件:

However you could simulate the key event with a piece of Javascript:

const SIMULATE_KEY =
  "var e = new Event(arguments[0]);" +
  "e.key = arguments[1];" +
  "e.keyCode = e.key.charCodeAt(0);" +
  "e.which = e.keyCode;" +
  "e.altKey = false;" +
  "e.ctrlKey = false;" +
  "e.shiftKey = false;" +
  "e.metaKey = false;" +
  "e.bubbles = true;" +
  "arguments[2].dispatchEvent(e);";

var target = driver.findElement(By.Id("..."));

// press the key "a"
browser.executeScript(SIMULATE_KEY, "keydown", "a", target);

// release the key "a"
browser.executeScript(SIMULATE_KEY, "keyup", "a", target);

这篇关于如何使用硒按住非修饰键(空格键)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆