5
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.

4

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

|improve this answer|||||
  • Could you please add more detail explaining how your solution improves on the solution the OP found and entered as an answer? – Kate Paulk Sep 1 '16 at 12:07
  • I have improved the answer. Hope it helps. – Vignesh Manogar Sep 2 '16 at 7:09
  • It helps - that makes this a good answer. – Kate Paulk Sep 2 '16 at 11:17
  • Doesn't work in FF for me — there's no .keys function in FF I think (now 2018). But element.setValue(`${value}\n`); as suggested in another answer here works fine. – KajMagnus Apr 28 '18 at 16:16
2

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
|improve this answer|||||
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();
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.