ivanleoncz
ivanleoncz

Reputation: 10065

window handles not updating on Python Selenium

I'm simulating a user interaction with Selenium, where I have basically, for example, 3 clicks on the navigation schema.

At each window, I'm mapping its window_handle and I realized, that current_window_handle is always returning the same identifier, and window_handles has always the same size, instead of having a list of window objects.

Therefore, I can't switch from window_3 to window_1, because current_window_handle is always the same...

Here's a sample in order to demonstrate the issue (the code which clicks on the links was suppressed, in order to focus the question on the code which I'm having trouble):

# click on a link, wait and get window handle
window_1 = browser.current_window_handle
print(window_1)
print(browser.window_handles)
print(browser.session_id)
browser.implicitly_wait(3)


# click on a link, wait and get window handle
window_2 = browser.current_window_handle
print(window_2)
print(browser.window_handles)
print(browser.session_id)
browser.implicitly_wait(3)

# click on a link, wait and get window handle
window_3 = browser.current_window_handle
print(window_3)
print(browser.window_handles)
print(browser.session_id)

And the result:

CDwindow-18D629A1BFC3391C5AE352F02768EFA3
['CDwindow-18D629A1BFC3391C5AE352F02768EFA3']
c50563aa58c1565d8c82ead6653e65a4

CDwindow-18D629A1BFC3391C5AE352F02768EFA3
['CDwindow-18D629A1BFC3391C5AE352F02768EFA3']
c50563aa58c1565d8c82ead6653e65a4

CDwindow-18D629A1BFC3391C5AE352F02768EFA3
['CDwindow-18D629A1BFC3391C5AE352F02768EFA3']
c50563aa58c1565d8c82ead6653e65a4

According to Selenium API Doc:

– current_window_handle

Usage:driver.current_window_handle

– window_handles

Returns the handles of all windows within the current session.

Usage:driver.window_handles

As you can see, the Session ID is always the same.

What could be possibly going wrong on this situation?

Upvotes: 0

Views: 1927

Answers (1)

supputuri
supputuri

Reputation: 14145

If you look at the output of print(browser.window_handles) it was always the same with only one list item.

['CDwindow-18D629A1BFC3391C5AE352F02768EFA3']

That indicates the link is loading/refresh the data in same window. So, you don't have to switch to any other tab.

Whenever, you see the new window/tab is opened then you can always switch to the new window before performing the operation on new window.

Switching to the latest window:

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

Upvotes: 1

Related Questions