Sending special keys to Watir-WebDriver

In vanilla Watir, if you wanted to send an enter key to a Watir browser, you’d do something like this:


To do the same thing in Watir-WebDriver, you do this:

 browser.send_keys :enter 

You can also do things like this:

browser.element.send_keys [:control, 'a'], :backspace

The full list of keys are available from here:

  • :null
  • :cancel
  • :help
  • :backspace
  • :tab
  • :clear
  • :return
  • :enter
  • :shift
  • :left_shift
  • :control
  • :left_control
  • :alt
  • :left_alt
  • :pause
  • :escape
  • :space
  • :page_up
  • :page_down
  • :end
  • :home
  • :left
  • :arrow_left
  • :up
  • :arrow_up
  • :right
  • :arrow_right
  • :down
  • :arrow_down
  • :insert
  • :delete
  • :semicolon
  • :equals
  • :numpad0
  • :numpad1
  • :numpad2
  • :numpad3
  • :numpad4
  • :numpad5
  • :numpad6
  • :numpad7
  • :numpad8
  • :numpad9
  • :multiply
  • :add
  • :separator
  • :subtract
  • :decimal
  • :divide
  • :f1
  • :f2
  • :f3
  • :f4
  • :f5
  • :f6
  • :f7
  • :f8
  • :f9
  • :f10
  • :f11
  • :f12
  • :meta
  • :command

Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.

23 thoughts on “Sending special keys to Watir-WebDriver”

  1. When I attempt this code:
    it totally messes up my keyboard and I have to restart the machine. Any idea why. For some reason Watir-Webdriver does not work on my machine. I’m trying to close a pop-up window by having watir press the ENTER key but am having no luck. Any help would be appreciated.


  2. We solved the problem using:
    Works like a charm!

    But to answer your question, the issue I’m having with watir-webdriver is that it opens firefox with a new profile each time. The IT gurus at my company have made it so that each new firefox profile begins with a proxy setting. I need that off to access the site(s) I wish to automate.

    I need a line of code or some other solution to fire up Firefox with no proxy settings. I’ve had no luck getting webdriver to use any browser of than Firefox.


  3. Thanks Alister. This looks very promising and I think it will solve my proxy problem. Awesome blog by the way. Really good information. I wish there were more people out there writing about Watir.


      1. Sorry, but probably I don’t understood you.
        I need to maximize a browser via keys combination ‘WIN+arrow up’ (please don’t propose other ways to do that because I know about that and use it).
        I just wanna to increase my knowledge :)

        the way you propose ‘, :control)’ calls an error which tells us that ‘click’ method has extra parameters.
        Sorry for that if my post is very primitive or stupid, but could you please explain me more detailed or just write an example.

        I use browser.send_keys (what should I write here to imitate keyboard keys combination WIN+arrow_up)


  4. I also need answer to the above question. How do we use keys combination. For ex: I want to maximize browser using :alt, :space and ‘x’. How do I use this comibination is send_keys method.


    1. If you want to press them at the same time you put them in an array:

      element.send_keys [:control, :f1]


  5. Has anyone recently tried this with chrome (chromedriver v23.xxxx)?

    I have successfully done this with firefox, but unable to do it in Chrome, even from the irb.

    Please help


    1. Tested now, and works fine for me
      Mac OS X, Ruby 1.9.3, Watir-WebDriver 0.6.1, Chrome 22

      1.9.3p194 :001 > require 'watir-webdriver'
      b = :chrome
      b.goto ""
      b.text_field(:name => 'q').set "hello"
      b.text_field(:name => "q").send_keys :enter


      1. can you try [:command, ‘v’]

        I actually posted on the git-hub for watir.
        Its also an existing issue in the chromedriver project.


        1. I just tested it again.

          single command works, the real problem is when you try to get 2 keys at once with [ ].

          i need to hit command+v (to paste)


          1. Why not just set the value in the field. cut and paste is a browser level operation, is it your job to test the browser?


Comments are closed.