Tag Archives: brackets

Utilizing shift and special characters with send_keys()

Today, had the most surprising blocker today.

Apparently, I’ve been automating for awhile and NEVER have had to send a shift-special-character key (the keys you access by pressing shift + normal keys such as !@#$%^&*() etc.)

[*NOTE — this is NOT the same as Keys characters which are sendable. http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver/selenium.webdriver.common.keys.html#module-selenium.webdriver.common.keys ]

I found out that you CAN’T send a shift-special character via send_keys. I think this is only a Firefox issue. In any case, after toying around with possible solutions, such as encoding/decoding, etc., here is my workaround.

SHIFT_KEYS = {
    "_" : Keys.SUBTRACT,
    "!" : '1',
    '@' : '2',
    '#' : '3',
    '$' : '4',
    '%' : '5',
    '^' : '6',
    '&' : '7',
    '*' : '8',
    '(' : '9'
}

def send_keys_filtered(driver, value, element):
    for i in range(len(value)):
        character = value[i]
        if character in SHIFT_KEYS:
            a = ActionChains(driver)
            a.key_down(Keys.SHIFT, element)
            a.send_keys(SHIFT_KEYS[character])
            a.key_up(Keys.SHIFT, element)
            a.perform()
        else:
            element.send_keys(character)


see https://dvcs.w3.org/hg/webdriver/raw-file/default/webdriver-spec.html#typing-keys

In essence, i ActionChain a shift + key if I encounter a shift character.