Switching between tabs

I am working on an automation project which requires me to switch between browser tabs. I keep getting the error “Window with last index same as current index” and I am not sure what is wrong with my logic.

I click the header image at the top right corner of the application which automatically directs the user to a new tab, then on the new tab there are links to different pages that I want to do some validation on, so I try to open a third tab, do the validation, close that tab then go back to the second tab to continue the process then eventually close the second tab to go back to the main page. I get the error as soon as I switch to tab3, what did I do wrong please? Thank you

Wait Until Page Contains Element    xpath://*[@id="mectrl_headerPicture"]    20
Click Element    xpath://*[@id="mectrl_headerPicture"]
Click Element    xpath://*[@id="mectrl_viewAccount"]
${tab2}    Switch Window    NEW
Wait Until Page Contains Element    xpath://a[contains(@href,'security-info')]    20
Click Element    xpath://a[contains(@href,'security-info')]
${tab3}    Switch Window    NEW
Page Should Contain    Security info
Close Window     ${tab3}
Switch Window    ${tab2}
........more validation........
Close Window    ${tab2}
Switch Window    MAIN

Hi Nina,

Based on the keywords your using I’ll guess you are using SeleniumLibrary?

From the documentation for Switch Window:

The previous windows handle is returned and can be used to switch back to it later.

So these lines:

${tab2}    Switch Window    NEW
# ...
${tab3}    Switch Window    NEW

Should actually be:

${tab1}    Switch Window    NEW
# ...
${tab2}    Switch Window    NEW

Also from the documentation for Close Window there is no argument, it just closes currently opened and selected browser window/tab.

But I’m not sure that is the solution to your issue? as you said

I get the error as soon as I switch to tab3

Which I think means you get the error on this line:

${tab3}    Switch Window    NEW

If that’s correct, again from the documentation:

If the locator is NEW (case-insensitive), the latest opened window is selected. It is an error if this is the same as the current window.

Perhaps this is the error you are getting, perhaps the line before (Click Element xpath://a[contains(@href,'security-info')]) didn’t actually cause a new tab to open:

Hopefully this helps,


I made the changes concerning ${tab1} and ${tab2} but why doesn’t it open the link I am selecting in a third tab if my logic is correct? That is why it is failing it clicks the link but does not open it in a new tab so I still get the error ‘Window with last index is same as current window’

Hi Nina,

Unfortunately, this is something I can’t know without access to the application.

  • It could be something as simple as a bug in the application? have you tried it manually?
  • it could be something weird like another element covering the one you are trying to click receiving the click so nothing happens?
  • it could be the a difference in browser version/type between the one you use manually and the one the automation is using?
  • it could be many other things I didn’t mention.

Start by repeating the steps manually make sure they do what you expect.

Then review the screen shot from the error, are you at the screen you expected to be at?

then consider adding Capture Page Screenshot at various points in the test especially before and after the Click Element xpath://a[contains(@href,'security-info')] and ${tab3} Switch Window NEW lines to make sure you are getting what you expected.

Once you figure out the issue and get the test working you can comment out or remove the Capture Page Screenshot lines you added.


1 Like