Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

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

Is there any way to connect and focus on the newly opened browser window?

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

1 Answer

0 votes
by (62.9k points)

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 the new window opened

for(String winHandle : driver.getWindowHandles()){

    driver.switchTo().window(winHandle);

}

 

// Perform the actions on a new window

// Close the new window if that window no more required

driver.close();

 

// Switch back to the original browser (first window)

driver.switchTo().window(winHandleBefore);

// Continue with the original browser (first window)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...