How to use the Key function from selenium-webdriver

Find comprehensive JavaScript selenium-webdriver.Key code examples handpicked from public code repositorys.

selenium-webdriver.Key is an interface for defining special keys on a keyboard that can be sent to a web element.

6
7
8
9
10
11
12
13
14
15
    ...capabilities['browser'] && { browserName: capabilities['browser']}  // Because NodeJS language binding requires browserName to be defined
  })
  .build();
await driver.get("http://www.google.com");
const inputField = await driver.findElement(webdriver.By.name("q"));
await inputField.sendKeys("BrowserStack", webdriver.Key.ENTER); // this submits on desktop browsers
try {
  await driver.wait(webdriver.until.titleMatches(/BrowserStack/i), 5000);
} catch (e) {
  await inputField.submit(); // this helps in mobile browsers
fork icon46
star icon585
watch icon14

47
48
49
50
51
52
53
54
55
56
Static implementation of [fill](#fill)

```js
this.W.fill({
  selector: ".field1",
  value: ["such good text", Driver.Key.ENTER]
})
```

### static hover
fork icon39
star icon531
watch icon47

+ 7 other calls in file

How does selenium-webdriver.Key work?

selenium-webdriver.Key is an enum representing the various keys that can be sent to a web element as a key press event, including keyboard keys, function keys, and modifier keys. The Key enum can be used to simulate user input via the sendKeys() method of the WebElement class.

4
5
6
7
8
9
10
11
12
var LogManager = require('./logManager');
var Helper = require('./helper');

//simplify webdriver usage
global.by = webdriver.By;
global.key = webdriver.Key;
global.promise = webdriver.promise;
global.bot = webdriver.error;
global.until = webdriver.until;
fork icon8
star icon134
watch icon14

+ 3 other calls in file

16
17
18
19
20
21
22
23
24
25
this.Button = webdriver.Button;
this.Capabilities = webdriver.Capabilities;
this.Capability = webdriver.Capability;
this.EventEmitter = webdriver.EventEmitter;
this.FileDetector = webdriver.FileDetector;
this.Key = webdriver.Key;
this.Session = webdriver.Session;
this.WebDriver = webdriver.WebDriver;
this.WebElement = webdriver.WebElement;
this.WebElementPromise = webdriver.WebElementPromise;
fork icon1
star icon0
watch icon2

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const { Builder, By, Key, until } = require("selenium-webdriver");

(async function example() {
  let driver = await new Builder().forBrowser("chrome").build();
  try {
    await driver.get("https://www.google.com");
    await driver.findElement(By.name("q")).sendKeys("webdriver", Key.RETURN);
    await driver.wait(until.titleIs("webdriver - Google Search"), 1000);
  } finally {
    await driver.quit();
  }
})();

In this example, the Key.RETURN constant is used to send the "Enter" key to the search input field.

241
242
243
244
245
246
247
248
249
	testOps.assertItemLabelIsHidden(1)
		.then(function () { done(); });
});

test.it('should save edits on enter', function (done) {
	page.editItemAtIndex(1, 'buy some sausages' + webdriver.Key.ENTER);
	testOps.assertItems([TODO_ITEM_ONE, 'buy some sausages', TODO_ITEM_THREE])
		.then(function () { done(); });
});
fork icon0
star icon0
watch icon1

+ 15 other calls in file

6
7
8
9
10
11
12
13
14
15
16
17
18
var classOrId = idSelectors ? '#' : '.';


var DEFAULT_TIMEOUT = 3000;
var REMOVED_TIMEOUT = 100;


var REMOVE_TEXT_KEY_SEQ = Array(51).join(webdriver.Key.BACK_SPACE + webdriver.Key.DELETE);


// Unique symbols
var ELEMENT_MISSING = Object.freeze({});
var ITEM_HIDDEN_OR_REMOVED = Object.freeze({});
fork icon0
star icon0
watch icon1

+ 3 other calls in file

40
41
42
43
44
45
46
47
48
49
Button = webdriver.Button;
Capabilities = webdriver.Capabilities;
Capability = webdriver.Capability;
EventEmitter = webdriver.EventEmitter;
FileDetector = webdriver.FileDetector;
Key = webdriver.Key;
Session = webdriver.Session;
WebDriver = webdriver.WebDriver;
WebElement = webdriver.WebElement;
WebElementPromise = webdriver.WebElementPromise;
fork icon0
star icon0
watch icon35

+ 5 other calls in file