Sherri
Sherri

Reputation: 23

Selenium - how to change driver instance from parent to child window

I have a situation in Selenium , where we are launching new browser instance using Internet explorer driver . normally the load URL is given in the same instance of IE that is launched this becomes the parent window , Now my issue is in parent window on click of a button a child window will open I need to give my second URL in child window which will continue to be my main page

The language we are using is Java Used the below code to switch window from parent to child window

public static boolean  handleMultipleWindows(WebDriver Driver) throws InterruptedException
{
    Thread.sleep(4000);
    String currentWindow= Driver.getWindowHandle();    // Storing parent window reference into a String Variable 
    System.out.println(" Check title " + Driver.getTitle() + Driver.getWindowHandles().size());
    for (String popUpHandle : Driver.getWindowHandles()) {  
        if(popUpHandle.equalsIgnoreCase(currentWindow))
            continue;
        Driver.switchTo().window(popUpHandle);
        String sTitle = Driver.getTitle();

but the problem in our application is the parent window gets close and when i try to play back the recording getting the below error org.openqa.selenium.WebDriverException: Unable to get browser (WARNING: The server did not provide any stacktrace information).

Upvotes: 0

Views: 5789

Answers (1)

Andrii
Andrii

Reputation: 357

Use method switchTo().window() method to switch between IE windows.

You didn't specify programming language that you use so my example is in Java:

//get window handlers as list
List<String> browserWindows = new ArrayList<String> (driver.getWindowHandles());
//switch to new window
driver.switchTo().window(browserTabs.get(1));
//do something
//then close window and get back
driver.close();
driver.switchTo().window(browserTabs.get(0));

Upvotes: 2

Related Questions