close

How do I switch to the active tab in Selenium?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do I switch to the active tab in Selenium in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How do I switch to the active tab in Selenium?

  1. How do I switch to the active tab in Selenium?

    This actually worked for me in 3.x:
    driver.switch_to.window(driver.window_handles[1])

  2. switch to the active tab in Selenium

    This actually worked for me in 3.x:
    driver.switch_to.window(driver.window_handles[1])

Method 1

This actually worked for me in 3.x:

driver.switch_to.window(driver.window_handles[1])

window handles are appended, so this selects the second tab in the list

to continue with first tab:

driver.switch_to.window(driver.window_handles[0])

Method 2

Some possible approaches:

1 – Switch between the tabs using the send_keys (CONTROL + TAB)

self.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

2 – Switch between the tabs using the using ActionsChains (CONTROL+TAB)

actions = ActionChains(self.driver)      
actions.key_down(Keys.CONTROL).key_down(Keys.TAB).key_up(Keys.TAB).key_up(Keys.CONTROL).perform()

3 – Another approach could make usage of the Selenium methods to check current window and move to another one:

You can use

driver.window_handles

to find a list of window handles and after try to switch using the following methods.

- driver.switch_to.active_element      
- driver.switch_to.default_content
- driver.switch_to.window

For example, to switch to the last opened tab, you can do:

driver.switch_to.window(driver.window_handles[-1])

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read