pythonselenium

How can I get the current window handle number?


I need to get the current window handle number (the 4), like this one:

driver.window_handles[4]

I'm already able to get the text using

driver.get_window_handle

But it gives me this string of text that I believe serves as an ID for the tab, right? Is there a simple way to get the number instead of this text?


Solution

  • I think this code should work. It just loops through all the window handles, checks if each handle is the current window handle, then assigns the index of that handle to current_window_handle_number

    tabs = driver.window_handles
    for window_handle in tabs:
         if window_handle == driver.current_window_handle:
              tab = tabs.index(window_handle)
              print(tab)