如何将关键组合发送到硒chromedriver?

时间:2018-05-18 14:25:56

标签: node.js selenium mocha selenium-chromedriver

我正在使用uirecorder创建mocha测试用例来测试我的网络程序。我想发送一些像“Metakey + R”这样的关键组合。但我无法做到这一点。

这是ui记录器生成步骤的一个例子:

it('sendKeys: {DOWN}', async function(){
    await driver.sendKeys('{DOWN}');
});

这完美无缺。但我无法弄清楚如何发送组合键。 问题是如何发送按键组合,如ctrl + a(按住ctrl然后按a然后离开ctrl)

我使用的解决方案:

我就是这样做的,并且工作正常。

await driver.sendKeys('{CTRL}a{CTRL}');

4 个答案:

答案 0 :(得分:1)

您可以使用ActionSequence类使用Node在selenium中执行操作。

Left control + a可以模拟鼠标操作:

new webdriver.ActionSequence(driver).keyDown(webdriver.Key.LEFT_CONTROL).sendKeys("a").keyUp(webdriver.Key.LEFT_CONTROL).perform();  

更多参考:
Reference 1

Reference 2

答案 1 :(得分:1)

我就是这样做的,并且工作正常。

await driver.sendKeys('{CTRL}a{CTRL}');

答案 2 :(得分:0)

使用Keys类:

String keypress = Keys.chord(Keys.CONTROL, "a");
driver.findElement(By.locator("value of locator")).sendKeys(keypress);

使用Actions类:

Actions action = new Actions(driver);  
action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();

答案 3 :(得分:0)

对于将来发现此问题的任何人,这里有一个对我有用的解决方案:

const {Key} = require('selenium-webdriver');

...

await driver.actions()
  .keyDown(Key.SHIFT)
  .sendKeys(Key.TAB)
  .keyUp(Key.SHIFT)
  .perform();