@@ -479,20 +479,27 @@ def simulate(self, locator, event):
479479 def press_key (self , locator , key ):
480480 """Simulates user pressing key on element identified by `locator`.
481481
482- `key` is either a single character, or a numerical ASCII code of the key
483- lead by ' \\ \\ '.
482+ `key` is either a single character, a numerical ASCII code of the key, \
483+ or a NAMED KEY as described at https://selenium.googlecode.com/git/docs/api/py/webdriver/selenium.webdriver.common.keys.html
484484
485485 Examples:
486- | Press Key | text_field | q | |
487- | Press Key | login_button | \\ \\ 13 | # ASCII code for enter key |
488- | Press Key | nav_console | \\ \\ \\ \\ ARROW_UP | # selenium.webdriver.common.keys ARROW_UP KEY |
489- """
490- if key .startswith ('\\ \\ ' ) and len (key ) > 1 :
491- key = getattr (Keys ,key [2 :])
492- elif key .startswith ('\\ ' ) and len (key ) > 1 :
493- key = self ._map_ascii_key_code_to_key (int (key [1 :]))
494- #if len(key) > 1:
495- # raise ValueError("Key value '%s' is invalid.", key)
486+ | Press Key | text_field | q | |
487+ | Press Key | login_button | \\ \\ 13 | # ASCII code for Enter key (DEPRECATED) |
488+ | Press Key | login_button | 10 | # ASCII code for Return key |
489+ | Press Key | nav_console | ARROW_UP | # selenium.webdriver.common.keys ARROW_UP KEY |
490+
491+ NAMED KEY value is new in Selenium2Library 1.7.3.
492+ """
493+ if len (key ) > 1 :
494+ if key .startswith ('\\ ' ):
495+ self ._warn ("Press Key: Escaped ASCII codes are deprecated. Use plain numeric value: '%s'" % (key [1 :]))
496+ key = self ._map_ascii_key_code_to_key (int (key [1 :]))
497+ else :
498+ try :
499+ key = (key .isdecimal () and self ._map_ascii_key_code_to_key (int (key ))) or \
500+ ((not key .isdecimal ()) and self ._map_named_key_code_to_special_key (key ))
501+ except :
502+ raise ValueError ("Key value '%s' is invalid." % (key ))
496503 element = self ._element_find (locator , True , True )
497504 #select it
498505 element .send_keys (key )
@@ -751,6 +758,15 @@ def _map_ascii_key_code_to_key(self, key_code):
751758 key = chr (key_code )
752759 return key
753760
761+ def _map_named_key_code_to_special_key (self , key_name ):
762+ try :
763+ return getattr (Keys , key_name )
764+ except :
765+ message = "Unknown key named '%s'." % (key_name )
766+ self ._debug (message )
767+ raise ValueError (message )
768+ return Keys .NULL
769+
754770 def _parse_attribute_locator (self , attribute_locator ):
755771 parts = attribute_locator .rpartition ('@' )
756772 if len (parts [0 ]) == 0 :
0 commit comments