Send multiple tab key presses with Selenium
Asked Answered
J

8

23

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?

Joinder answered 13/2, 2016 at 20:56 Comment(0)
H
37

Use Action Chains:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

N = 5  # number of times you want to press TAB

actions = ActionChains(browser) 
for _ in range(N):
    actions = actions.send_keys(Keys.TAB)
actions.perform()

Or, since this is Python, you can even do:

actions = ActionChains(browser) 
actions.send_keys(Keys.TAB * N)
actions.perform()
Homer answered 13/2, 2016 at 20:58 Comment(3)
in my case I needed to import Keys: from selenium.webdriver.common.action_chains import KeysNectarine
for me it's from selenium.webdriver.common.keys import KeysRabato
I can move the cursor with the send_keys(Keys.TAB) to the OK button, but the send_keys(Keys.ENTER) doesn't do anything. The focus is not on the file download dialog.Hiram
S
5

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.

Serif answered 13/2, 2016 at 23:29 Comment(0)
H
1
uname.send_keys(Keys.TAB, Keys.TAB, Keys.TAB..)

worked for me.

Hectogram answered 20/9, 2019 at 7:13 Comment(2)
Literally "Keys.TAB.."?Rajewski
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.Marauding
R
1

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.

Rumilly answered 9/5, 2020 at 13:58 Comment(1)
at least someone is aware...Explication
V
0

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

Vexatious answered 8/6, 2020 at 22:9 Comment(0)
J
0

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.

Jumbo answered 17/6, 2020 at 8:5 Comment(0)
E
0

Try to follow each send_keys with switchto like this -

for i in range(10):
    elem.send_keys(Keys.TAB)
    elem = driver.switchTo().activeElement()
Elbert answered 9/5, 2022 at 7:3 Comment(0)
L
0

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.

Luxor answered 29/5, 2022 at 4:7 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Braunschweig
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewGnotobiotics

© 2022 - 2024 — McMap. All rights reserved.