user15953688
user15953688

Reputation:

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?

Upvotes: 1

Views: 1046

Answers (2)

user22396757
user22396757

Reputation: 1

this should work

current_handle_index = browser.window_handles.index(browser.current_window_handle)

Upvotes: 0

C. Peck
C. Peck

Reputation: 3711

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)
        

Upvotes: 1

Related Questions