Med.J
Med.J

Reputation: 71

simulate key press in selenium webdriverjs

I am using Selenium WebDriverJS to write a test. Now I need to press the key down in keyboard. Is it possible to simulate key press on the selenium webdriverJS ? if yes how ?

In java we do this :

driver.findElement(Locator).sendKeys(Keys.ARROW_DOWN);

Thank you.

Upvotes: 1

Views: 13425

Answers (4)

SuperSmartWorld
SuperSmartWorld

Reputation: 111

Yes, you can achieve that by an ActionSequence. There are several functions, but keyDown(key) and keyUp(key) are only used for modifier keys. The one you need is sendKeys(...var_args)

var element1 = driver.findElement(<locator1>);
driver.actions().click(element1).sendKeys(Key.ARROW_DOWN).perform();

You can even pass multiple keys

var element2 = driver.findElement(<locator2>);
driver.actions().click(element2).sendKeys(Array(3).fill(Key.ARROW_UP), Key.ENTER).perform();

A complete list for all possible Keys you can find here: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/input_exports_Key.html

For more details just read here: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/actions_exports_LegacyActionSequence.html

Upvotes: 2

Rajnish Kumar
Rajnish Kumar

Reputation: 2938

Yes its possible, try it in this way:

new webdriver.ActionSequence(driver).
    keyDown(webdriver.Key.SHIFT).
    click(element1).
    click(element2).
    dragAndDrop(element3, element4).
    keyUp(webdriver.Key.SHIFT).
    perform();

For more keys have a look at

https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/lib/actions.js#L43

Unfortunately there is no ARROW_DOWN key as you can see

Upvotes: 0

Curious
Curious

Reputation: 282

Med.J - update your code as below:

    Robot robot3 = new Robot();
    robot3.keyPress(KeyEvent.VK_PAGE_DOWN);
    robot3.keyRelease(KeyEvent.VK_PAGE_DOWN);

Upvotes: 1

noor
noor

Reputation: 3004

for down arrow click, use below:

keys('Down arrow');

Upvotes: 0

Related Questions