Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.JS selenium send key enter

when i send a the reutn key in nodejs it gives a error?

driver.findElement(By.id('twofactorcode_entry')).sendKeys(Keys.ENTER);

All the sites tell me this is what i need to use? any help please

like image 975
Sam H Avatar asked Jan 02 '17 14:01

Sam H


2 Answers

I'm assuming you are using npm package Selenium Webdriver: https://www.npmjs.com/package/selenium-webdriver

The error Keys is not defined occurs because you haven't defined keys before using it. You need to use the Enum Key (https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_Key.html)

It is a constant on the driver module and exported as Key (note the singular form). I've put the updated code below.

const driver = require('selenium-webdriver')

driver.findElement(By.id('twofactorcode_entry')).sendKeys('webdriver', driver.Key.ENTER);

Extra tip: I've been using http://webdriver.io/ and it's a little easier to use than this library

like image 195
Peter Grainger Avatar answered Oct 09 '22 20:10

Peter Grainger


For some reason the other solutions didn't work for me.

Using Node.js, it seems you can just use \n for enter:

await driver.findElement(By.id('my-field-id')).sendKeys('my-value\n');

like image 22
corolla Avatar answered Oct 09 '22 20:10

corolla