Unable to switch to window with index: '1'

On a web page, the script clicks on a link and a new window opens up. While trying to switch to this window, the following exception is displayed:

indent preformatted text by 4 spaces
2020-09-08 17:09:44.431 ERROR c.k.k.core.keyword.internal.KeywordMain - :x: Unable to switch to window with index: ‘1’ (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to switch to window with index: ‘1’

at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)

at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)

at com.kms.katalon.core.webui.keyword.builtin.SwitchToWindowIndexKeyword.switchToWindowIndex(SwitchToWindowIndexKeyword.groovy:85)

at com.kms.katalon.core.webui.keyword.builtin.SwitchToWindowIndexKeyword.execute(SwitchToWindowIndexKeyword.groovy:67)

at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:73)

at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.switchToWindowIndex(WebUiBuiltInKeywords.groovy:1874)

at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$switchToWindowIndex$4.call(Unknown Source)

at 1.1 Verify login landing page.run(1.1 Verify login landing page:66)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:339)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:330)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:309)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:301)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:235)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at TempTestCase1599565133335.run(TempTestCase1599565133335.groovy:25)

Caused by: org.openqa.selenium.InvalidArgumentException: Expected “handle” to be a string, got [object Undefined] undefined

Based on the error reported by the script, I really need to see your code.

When trying to switchToWindowIndex(1), the script is failing.

I am using firefox 80.0.1 as default browser. It is only failing for firefox. Script is passing for safari. But I want to use firefox to execute my other scripts. Can you please guide me?

WebUI webUI = new WebUI()
webUI.openBrowser(GlobalVariable.applicationUrl, FailureHandling.STOP_ON_FAILURE)
webUI.waitForPageLoad(5, FailureHandling.STOP_ON_FAILURE)
webUI.click(findTestObject(‘Object Repository/1/a_Terms of Use’))
webUI.delay(5)
webUI.switchToWindowIndex(1)
webUI.verifyMatch(webUI.getUrl(), GlobalVariable.expectedUrl, false, FailureHandling.CONTINUE_ON_FAILURE)

webUI.closeBrowser()

Two things…

  1. When you post your code, use copy-paste.

  2. WebUI.switchToWindowIndex() definitely works – I use it with Firefox every day.

Based on the error, it sounds like you’re passing the wrong argument to a browser window API (switchToWindowIndex in this case). Based on your code I cannot see how that error is caused.

Try updating your web drivers: Tools > Update WebDrivers

I just noticed you are instantiating a new WebUI instance. There is no need to do that. Just use WebUI.X directly. It may not solve your problem but it will clean up your code.

Hi @pa_jen
This issue in an known issue of gecko driver and Firefox v80: Firefox 80: Expected "handle" to be a string, got [object Undefined] undefined · Issue #1771 · mozilla/geckodriver · GitHub.

You should downgrade Firefox to v79 and wait for another Firefox release.

1 Like

@duyluong Actually, it appears the thread was closed because the respondents had the wrong version of geckodriver installed:

So @pa_jen, please update your drivers like I said before.

1 Like

Some issues, but with Crome (Headless)
Running tests in Jenkins + Headless:

In 8 tests using WebUI.switchToWindowIndex(1)
But only 1 test failed with errors: com.kms.katalon.core.exception.StepFailedException: Cannot find window with index: ‘1’
Cannot find window with index: ‘1’ (Root cause: com.kms.katalon.core.exception.StepFailedException: Cannot find window with index: ‘1’

@duyluong @ThanhTo

This bit me today.

What about Chrome(Headless) ?

test failed with errors: com.kms.katalon.core.exception.StepFailedException: Cannot find window with index: ‘1’

When I’m trying to execute my testcase in headless browser

@a.polovinkin i will let @Russ_Thomas to speak about it…
but, for the current issue, as far as i understand, the root cause was a side effect of mozilla driver not being up-to-date.
So, if your use case lands here, kindly try to update your web driver
If not, please open new issue, it may be same or not in the case of chrome driver

There’s nothing much I can add. @duyluong says’t it’s fixed. I tested the driver update feature and it’s working.

It is still continue on katalon docker image.

com.kms.katalon.core.exception.StepFailedException: Unable to switch to window with index: ‘1’

@Ramazan I found this on this forum on another question. How about you try it?

int currentTab = WebUI.getWindowIndex()

WebUI.switchToWindowIndex(currentTab + 1)

@grylion54 didn’t work. Updating firefox driver to 83 on local is solved the issue. Can u update docker image accordingly? Or i can make a pull request.