How to switch between chrome windows opened by different webdriver in Java?

Viewed 239
3 Answers

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.

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();");

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