Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 677
Volodymyr Prysiazhniuk Avatar asked Mar 06 '12 17:03

Volodymyr Prysiazhniuk


People also ask

How u will switch to different windows?

You can use Flip to switch between open windows. To do this, press and hold the Alt key on your keyboard, then press the Tab key. Continue pressing the Tab key until the desired window is selected.

How can we switch to a new window in Selenium?

Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.

How do I open a new browser in Selenium?

Open a New Tabget('https://selenium.dev'); // A new tab is opened and switches to it await driver. switchTo(). newWindow('tab'); // Loads Sauce Labs open source website in the newly opened window await driver. get('https://opensource.saucelabs.com/');


1 Answers

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) 
like image 107
Surya Avatar answered Sep 17 '22 15:09

Surya