Home > Software engineering >  How to switch between chrome windows opened by different webdriver in Java?
How to switch between chrome windows opened by different webdriver in Java?

Time:11-18

When opening browsers using the same driver instance the switch works (followed https://www.browserstack.com/guide/handle-multiple-windows-in-selenium)but when I try to open two different windows with two different instance of web driver how to make it work? I want to switch control to the first opened chrome window. Please help.

CodePudding user response:

You can not do that. These are 2 different, totally separated web driver instances (processes) and you can not switch from one to another or pass windows handles between them during their run.

CodePudding user response:

To switch between two Browsing Contexts e.g. two windows opened by two different WebDriver you can switch the focus using the following lines of code:

((JavascriptExecutor) driverA).executeScript("window.focus();");
((JavascriptExecutor) driverB).executeScript("window.focus();");

CodePudding user response:

You can use WinAppDriver if you do not have another option, it deals with window handles. It is used mainly for desktop applications. It works for web applications, but is more time-consuming.

I am not aware of such option in Selenium. You can attach to already running Chrome driver, but as far as I know, there is no such functionality for the other drivers.

References:
How to switch focus between windows using WinAppDriver Java
https://harith-sankalpa.medium.com/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd

  • Related