Trying to do accessibility automation and want to use shift+tab to go back an element. But can't seem to get anything to work. Tried what the doc imply should work based off their "Example, pressing ctrl+c:":

action = ActionChains(driver)
action.key_down(Keys.SHIFT)
action.send_keys(Keys.TAB)
action.key_up(Keys.SHIFT)
action.perform();

Then I guessed at :

action = ActionChains(driver)
action.key_down(Keys.SHIFT).key_down(Keys.TAB)
action.key_up(Keys.SHIFT).key_up(Keys.TAB)
action.perform();

And:

action = ActionChains(driver)
action.key_down(Keys.SHIFT)
action.key_down(Keys.TAB)
action.perform();
action = ActionChains(driver)
action.key_up(Keys.SHIFT)
action.key_up(Keys.TAB)
action.perform();

And:

action = ActionChains(driver)
action.send_keys(Keys.TAB + Keys.SHIFT).perform();

And:

ActionChains(driver).send_keys(Keys.TAB).perform() # Tabs 1 forward
time.sleep(1)
ActionChains(driver).send_keys(Keys.SHIFT, Keys.TAB).perform() # Tabs 1 backwards
time.sleep(1)
ActionChains(driver).send_keys(Keys.SHIFT, Keys.TAB).perform() # Tabs 1 forward
time.sleep(1)

But none of them work. At least at selenium 3.14 & chrome 71.


So how do I hit shift and tab at the same time in Python Webdriver?

That's the signature for send_keys:

send_keys(*keys_to_send)

The asterisk means it expect a variable number of arguments. So, you should do something like:

send_keys(Keys.ALT, Keys.TAB)

Or maybe a combination of key_down and key_up.

  • So this kinda worked but not for the purposes I need. In the interest of keeping the code formatted I added it as the last example to my question. But sending them comma separated goes back 1 element but then if you do it a 2nd time it only goes forward instead. – Cynic Dec 8 at 16:40
  • 1
    Of I get it, for shift you have to send it again to release. Will you please update your answer to include for shift tab the release. ActionChains(driver).send_keys(Keys.SHIFT, Keys.TAB, Keys.SHIFT).perform(). – Cynic Dec 8 at 16:48

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.