0

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?

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
  • Why would "click on a link" create additional window (handle)? – SiKing Jun 17 '19 at 22:02
  • I suppose that, when click a link for another screen, it should generate another window handle on `window_handles`. Or am I wrong? – ivanleoncz Jun 17 '19 at 22:03
  • Does the link force a new tab / window to open? Or is just a plain link? – SiKing Jun 17 '19 at 22:09
  • It's just a link for opening a new screen on the same tab. I have a final step, which is a popup window, which requires a different approach, but for these steps, I'm always getting the same object for the current window handle. I suppose that, `window_handles` is a list, which naturally contains the objects for the windows that the WebDriver has processed after clicks (depending on the case), in theory. – ivanleoncz Jun 17 '19 at 22:13
  • As you have observed from your experiment: a plain link that does not create a new window, will not create any additional window handles. – SiKing Jun 17 '19 at 22:16
  • Yeap.. I guess so :( – ivanleoncz Jun 17 '19 at 22:19
  • In my innocence (for not saying ignorance), I thought that `window_handles` would contain snapshots of my navigation, and now I realize, that it is the management of "true" windows that could be opened during the navigation. I made some other tests here, before writing this. Would you mind to put an answer here :)? – ivanleoncz Jun 17 '19 at 22:42
  • @ivanleoncz Does [this discussion](https://stackoverflow.com/questions/53690243/selenium-switch-tabs/53692407#53692407) helps you? – undetected Selenium Jun 18 '19 at 09:27

1 Answers1

1

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])
supputuri
  • 13,644
  • 2
  • 21
  • 39