'Send multiple tab key presses with Selenium

How can I send multiple tabs with Selenium?

When I run:

uname = browser.find_element_by_name("text")
uname.send_keys(Keys.TAB)

the next element is selected. When executing uname.send_keys(Keys.TAB) again nothing happens - actually the next element from uname is selected → so it is the same as when running it once.

How can I jump forward multiple times - basically as I would press TAB manually multiple times?



Solution 1:[1]

I think you can also write

uname.send_keys(Keys.TAB + Keys.TAB + Keys.TAB + ... )

It may be useful if you have only two or three commands to send.

Solution 2:[2]

As the OP states: "actually the next element from uname is selected".

After the first <TAB> key you have moved off the element, so no further <TAB>s will be recognized by that element. You need to locate the parent element and send keys to it.

Solution 3:[3]

uname.send_keys(Keys.TAB, Keys.TAB, Keys.TAB..)

worked for me.

Solution 4:[4]

sendkeys(Keys.Tab, Keys.Tab, Keys.Tab) is working fine.

Solution 5:[5]

This syntax saved me:

ActionChains(driver).send_keys(Keys.TAB * 2).perform()

I tried using this from the accepted answer:

actions = ActionChains(browser)
actions.send_keys(Keys.TAB * 2)
actions.perform()

But since I wanted to later use three TABs in the same script, I ran into problems. The thing is that actions.send_keys(Keys.TAB * 3) simply adds to the previous lines in actions in the same script. So after the second time I use this line, instead of desired three TAB keys pressed I get five (i.e. 2 + 3). Furthermore, ActionChains.reset_actions() does not seem to work.

Solution 6:[6]

Try to follow each send_keys with switchto like this -

for i in range(10):
    elem.send_keys(Keys.TAB)
    elem = driver.switchTo().activeElement()

Solution 7:[7]

In truth, to understand why send_keys isn't working with your html, it would be useful to see the html that you've got rendered. Go to the page with the element that you're trying to test, and right-click on the element, then select 'Inspect', and copy/paste that into your question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen
Solution 2 jcomeau_ictx
Solution 3 Peter Mortensen
Solution 4 Peter Mortensen
Solution 5 Peter Mortensen
Solution 6 Ray Lee
Solution 7 raydleemsc