How to switch to the new browser window, which opens after click on the button? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to switch to the new browser window, which opens after click on the button? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have situation, when click on button opens the new browser window with search results.

Is there any way to connect and focus to new opened browser window?

And work with it, then return back to original(first) window.

Answer

You can switch between windows as below:

// Store the current window handle
String winHandleBefore = driver.getWindowHandle();

// Perform the click operation that opens new window

// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

// Perform the actions on new window

// Close the new window, if that window no more required
driver.close();

// Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);

// Continue with original browser (first window)
We are here to answer your question about How to switch to the new browser window, which opens after click on the button? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji