Home > Back-end >  How do I store the ID of the current tab and switch to this tab?
How do I store the ID of the current tab and switch to this tab?

Time:05-24

I opened a completely new window and three tabs.

driver.get('link1')
driver.switch_to.new_window('tab')
driver.get('link2')
wait.sleep(2)
driver.switch_to.new_window('tab')
driver.get('link3')

How can I store the id of the tab1, tab2, tab3, so I can programmatically switch between them?

I tried this:

  original_window = driver.current_window_handle

CodePudding user response:

You didn't describe what problem you had with current_window_handle so I don't understand your problem. If you got some error message so you should show it in question.


I have no problem to create (many) tabs and get IDs/handlers

driver.switch_to.new_window('tab')

tab_id = driver.current_window_handle

and later switch to this tab

driver.switch_to.window(tab_id)

driver.get('link2')

And all this works even if I create tabs in new window created with

driver.switch_to.new_window('window')

Doc Working with windows and tabs

BTW: in documentation in first sentence you can see

"WebDriver does not make the distinction between windows and tabs."

Minimal working code which first creates 4 empty tabs and use driver.current_window_handle to gets ID/handler and keep on list. And later it uses this list to switch tabs and load 4 different urls.

It works even if I manually switch (or reoder) tabs in window.

Tested on:

  • Linux Mint 20
  • Python 3.8
  • Selenium 4.1.3
  • browsers: Firefox, Chrome, Microsoft Edge.

I used module webdriver_manager to automatically download fresh driver for current browser.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
#from webdriver_manager.firefox import GeckoDriverManager
#from webdriver_manager.microsoft import EdgeChromiumDriverManager

import selenium
print('Selenium:', selenium.__version__)

all_urls = [
    'https://httpbin.org/get',
    'https://toscrape.com/',
    'https://books.toscrape.com/',
    'https://quotes.toscrape.com/',
]

all_tabs = []

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
#driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))
#driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()))

#print('--- create second window ---')
#driver.switch_to.new_window('window')

print('--- create empty tabs ---')

for number in range(len(all_urls)):
    driver.switch_to.new_window('tab')
    tab_id = driver.current_window_handle
    all_tabs.append(tab_id)
    print(f'tab{number 1}:', tab_id)

print('--- switch tabs to load urls ---')

for number, (tab_id, url) in enumerate(zip(all_tabs, all_urls), 1):
    driver.switch_to.window(tab_id)
    driver.get(url)
    print(f'tab{number}:', driver.current_window_handle, url)
    
#  now you can manually switch/reorder tabs and rerun last for-loop 
  • Related