2

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
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • Do these help?: https://stackoverflow.com/questions/57368346/how-to-get-chrome-tab-id-in-selenium, https://stackoverflow.com/questions/7303452/how-to-get-current-tabid-from-background-page and https://stackoverflow.com/questions/58460526/is-it-possible-to-use-webdriver-selenium-to-get-current-tab-which-user-actually – The Amateur Coder May 22 '22 at 20:24
  • These may not work when you drag and drop/change the order of the tabs (because the index changes). You can try getting the tab's title or some other details which are unique to it. Or try assigning a value to every tab that's created so you can refer to the value to get/switch to the tab. – The Amateur Coder May 22 '22 at 20:27
  • @TheAmateurCoder OP didn't show full working code so I can't run it and see if it gives error. OP tried `current_window_handle` but OP didn't described results. If OP got error then OP should show all details - because we can't run this code and we can't read in his mind. Frankly, I created my code and it works without any problems so I have no idea what problem has OP - because OP didn't describe it. BTW: it seems I wrote it to your comment which is already deleted. – furas May 23 '22 at 11:30
  • better show minimal working code which makes problem and if you get error then show it in question (not in comment) as text (not images). It created own code with `current_window_handle` and it works without problems. – furas May 23 '22 at 11:36
  • @furas, I feel there's no need for any "full working code" as `driver.current_window_handle` is self-explanatory: using it will only give the driver's window's handle and not a specific tab's 'ID'/anything unique to the tab that can be used to refer to it. I, too, frankly, haven't used Python Selenium much, but it's easy to guess that it won't produce any error and that it will return something else instead. If you meant to say that you created *some* code with `current_window_handle` and *it* *works* without *problems*, I think you forgot to read the title twice. – The Amateur Coder May 23 '22 at 11:51
  • @TheAmateurCoder `current_window_handle` gives some kind of ID (you can call it handler) which I use later with `switch_to` to change tab - and I don't understand why OP has problem with this. And this is why I ask to show full working code to understand OP problem. – furas May 23 '22 at 11:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244954/discussion-between-the-amateur-coder-and-furas). – The Amateur Coder May 23 '22 at 11:57
  • @TheAmateurCoder by the way: at this moment nobody put answer so maybe OP should better describe problem. But I skip this question and finish comments. – furas May 23 '22 at 12:02
  • @furas The OP wants to switch to a specific tab and not the window. The OP probably wants to switch back to it even if the tabs have been re-ordered. It probably worked for you because you opened only 1 tab. Also, what's the ID that you got? – The Amateur Coder May 23 '22 at 12:03
  • The question is good, perhaps too good to be answered. The answer is probably a workaround. You can leave it if you want, but I don't think you got what the OP wants. `¯\_(ツ)_/¯` – The Amateur Coder May 23 '22 at 12:05
  • 1
    @TheAmateurCoder I tested on 4 tabs, first I created tabs and use `current_window_handle` to get `handler` and keep on list, and later I use this list to switch tabs and use `get()` to load 4 different urls. ANd later I change manualluy order of tabs and use again handlers and switch to load other urls - and it works for me without any problem. And it works with tabs, not windows. I tested it with Firefox and Chrome. And this is why I don't undestand what problem has OP - and without details there is no answer. – furas May 23 '22 at 12:30

1 Answers1

1

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 
furas
  • 134,197
  • 12
  • 106
  • 148