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:

 browser.send_keys("{ENTER}") 

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

23 thoughts on “Sending special keys to Watir-WebDriver

  1. When I attempt this code:
    browser.send_keys(“{ENTER}”)
    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:
    $browser.javascript_dialog.button(‘OK’).click
    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.

    • There’s a page on tweaking firefox prefs for Watir-WebDriver

      something like:

      
      profile = Selenium::WebDriver::Firefox::Profile.new
      proxy = Selenium::WebDriver::Proxy.new(:http => "http://proxy.org:8080")
      profile.proxy = proxy
      
      driver = Selenium::WebDriver.for :firefox, :profile => profile
      

      See: http://code.google.com/p/selenium/wiki/RubyBindings

  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.

  4. Hello. How to press keys combinations like “Win + any key”?
    I need to press at the same time the ‘Windows’ key and ‘arrow up’
    Anybody helps me?

      • 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 ‘b.element.click(:shift, :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)

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

  6. how can i press control + F1?

    element.send_keys(:control, ‘f1) does not work.!!!

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

      element.send_keys [:control, :f1]
      
  7. 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

    • 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 = Watir::Browser.new :chrome
      b.goto "google.com"
      b.text_field(:name => 'q').set "hello"
      b.text_field(:name => "q").send_keys :enter
      
      • can you try [:command, ‘v’]

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

        • 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)

          • 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?

  8. I wanna hold the right key for 5 sec, please tell me how …..please….

Comments are closed.