6
var webdriverio = require('webdriverio');
var options = {
    desiredCapabilities: {
        browserName: 'firefox'
    }
};

webdriverio
    .remote(options)
    .init()
    .url('http://google.com')
.setValue('.gsfi', 'webdriver')
//.pause(3000)
.keys('Enter')
//    .click('#btnG')

    .getTitle().then(function(title) {
        console.log('Title was: ' + title);
    })
    .end();

The code opens google.com, enters webdriver, but .keys('Enter') does not send enter. Instead the webbrowser remains open and nothing happens. Another attempt was .click('#btnG'), but nothing happens.

030
  • 201
  • 1
  • 2
  • 6

3 Answers3

6

The parameter passed inside 'keys' function is just a array of characters to be typed. For actions like enter and backspace, appropriate unicode characters must be passed. The unicode character for 'enter' is '\uE007'.

Specific answer to the question:

browser.keys("\uE007"); 

Answer from answer '030' is just an alternate way to get the search going. Clicking on the search button by passing the selectors for search button to the click function.

For further documentation on the other keyboard actions: https://w3c.github.io/webdriver/webdriver-spec.html#h-keyboard-actions

alecxe
  • 11,425
  • 10
  • 49
  • 107
3

I'm using following function to type some text and click enter after it (using webdriverio 4.0+ with new synchronous syntax):

function setValue(browser, element, value) {
  if (browser.options.desiredCapabilities.browserName == "MicrosoftEdge") {
    element.setValue(value);
    browser.keys("\uE007");
  } else {
    element.setValue(`${value}\n`);
  }
}

It helps with following problems:

  • element.setValue doesn't work for Edge (the \n character is ignored as enter key wasn't pressed) so we're using browser.keys
  • browser.keys doesn't work for Firefox driver (Failed: sendKeysToActiveElement) and it's deprecated so we're using element.setValue for non-Edge browsers
alecxe
  • 11,425
  • 10
  • 49
  • 107
0

Concise

A snippet from this example fixed the issue:

.setValue('*[name="q"]','webdriverio')
.click('*[name="btnG"]')

Verbose

var webdriverio = require('webdriverio');
var options = {
    desiredCapabilities: {
        browserName: 'firefox'
    }
};

webdriverio
    .remote(options)
    .init()
    .url('http://google.com')
    .setValue('*[name="q"]','webdriverio')
    .click('*[name="btnG"]')
    .pause(3000)
    .getTitle().then(function(title) {
        console.log('Title was: ' + title);
    })
    .end();
030
  • 201
  • 1
  • 2
  • 6