How do you send special keys in Selenium Library/Robot Framework?

Viewed 11712

I am trying to send F12 using in Robot Framework, probably by using Selenium Library's send special key.

I found this page that lists keycodes for common uncommon keys https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.keys.html where F12 is listed as \ue03c.

I have tried all combinations I could imagine of backslashes and quotations but none of them work. How do I send F12 in Robot Framework?

Furthermore, in the next step I need to send F16 but it is not listed in the linked page - how do I send that?

Edit: I don't need to send the key to a specific element or so, I just want to trigger a menu option using its keyboard shortcut.

Edit 2: I am limited to Selenium Library 3.1.

2 Answers

From SeleniumLibrary 3.3 Press Keys is introduced with the following functionality:

Simulates user pressing key(s) to an element or on the active browser.

This allows the user to send special keys or key combinations to the browser itself, beside web elements.

Sleep   5s
Press Keys  //*[contains(@id,'textfield')]    AAAAA    # enter text "AAAAA" to a textfield
Sleep   5s
Press Keys  None    F3    # F3 into browser
Sleep   5s
Press Keys  None    CTRL+Z   # Send CTRL+Z to browser to Undo "AAAAA" in the textfield
Sleep   5s

Here is the output log for the three Press Keys calls:

  • first one simply types into the textfield:

    KEYWORD SeleniumLibrary.Press Keys //*[contains(@id,'textfield')], AAAAA
    Documentation: Simulates user pressing key(s) to an element or on the active browser.
    INFO    Sending key(s) ('AAAAA',) to //*[contains(@id,'textfield')] element.    
    INFO    Sending keys AAAAA
    
  • second one sends special key to the browser:

    KEYWORD SeleniumLibrary.Press Keys None, F12
    Documentation: Simulates user pressing key(s) to an element or on the active browser.
    INFO    Sending key(s) ('F12',) to page.    
    INFO    Pressing special key F12 to browser.    
    
  • third one, presses and keeps CONTROL down, send a Z, then releases CONTROL. Doing an Undo action all in all. All sent to the page.

    KEYWORD SeleniumLibrary.Press Keys None, CTRL+Z
    Documentation: Simulates user pressing key(s) to an element or on the active browser.
    INFO    Sending key(s) ('CTRL+Z',) to page.     
    INFO    Pressing special key CONTROL down.  
    INFO    Sending key Z   
    INFO    Releasing special key CONTROL.
    

As for the F16, according to this page, SHIFT+F4=F16, or at least on Windows.

You can use the seleniumlibrary built in keyword Press Key along with the corresponding ASCII code of the certain key you want to press.

For your question if you wanted to send "F12" you would do the following.

press key    (An element on the page)    \\86

86 is the ASCII code for F12, and the documentation for this keyword explicitly says for it to be called on an element of the page. You can read more about it here.

http://robotframework.org/Selenium2Library/Selenium2Library.html#Press%20Key

Related