1

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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.