Pages

Sunday, 19 February 2012

How can I switch WebDriver control to new window?

To switch latest window opened use below code:

for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

Sampe code:


 package test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SwithcToNewWindow {

public static void main(String[] arg) {

// Instantiate the FirefoxDriver
WebDriver driver = new FirefoxDriver();

// Visit the Selenium-WebDriver FAQ site
driver.get("http://seleniumwebdriverfaq.tumblr.com/");

// Print the title of the page - It should print
// "Selenium-WebDriver FAQ's"
System.out.println("Title of the page before - switchingTo: " + driver.getTitle());

//Now click on RSS button; it will open in new window
driver.findElement(By.xpath("//a[text()='RSS']")).click();

//Switch to newly opened window -RSS and get the page titele
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
System.out.println("Title of the page after - switchingTo: " + driver.getTitle());

// Close the browser window
driver.close();

// Quit the driver
driver.quit();
}
}

14 comments:

  1. for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
    }
    System.out.println("Title of the page after - switchingTo: " + driver.getTitle());

    If same could have been written as

    for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);

    //In case title change with new window name new title name can be known with below statement
    System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
    }
    Thanks,
    Nellore krishna kuma

    ReplyDelete
    Replies
    1. Your code will just prints all the windows available and switching operation remains the same. That is, it will switch driver to latest window.

      If you know the title of the window; then you can use the switch statement as below:

      for(String winHandle : driver.getWindowHandles()){
      driver.switchTo().window(winHandle);
      //Break the loop if required window found.
      if(driver.getTitle().equals("expectedPageTile")){
      System.out.println(" You are in required window");
      break;
      }
      }

      Delete
    2. Its not working for me. I have tried to use above command but didnt worked.
      driver.get("URL");
      System.out.println("Title of the page before - switchingTo: "
      + driver.getTitle());
      driver.findElement(By.id("OpenPopupButton")).click();

      for(String winHandle : driver.getWindowHandles()){
      driver.switchTo().window(winHandle);
      //Break the loop if required window found.
      if(driver.getTitle().equals("Child Window")){
      System.out.println(" You are in required window");
      break;
      }
      }
      System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
      }

      Output: "Title of the page before - switchingTo: Parent Window".
      After closing the popup window manually i got the "Title of the page after - switchingTo: Parent Window"

      Please help

      Delete
    3. I too had the same issue. Yet to resolve

      Delete
  2. Great! It works well.

    ReplyDelete
  3. i have two windows with the same title of page. How to handle it?

    ReplyDelete
  4. Am not able to switch the control to new window. help me to switch the control

    Below is my code:

    driver.findElement(By.id("tabFireRecords__ctl1_UsrIncidentNames1_btnAddNew")).click();// to click on the elements which opens the new window

    for(String winHandle : driver.getWindowHandles())
    {
    driver.switchTo().window(winHandle);

    //In case title change with new window name new title name can be known with below statement
    System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
    }




    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Can anyone help me in the below scenario :

    How to handle a Pop up window where as it acts as Main window once login into my project. All the operations wil be performed on the Pop Up window.

    After login main Pop Window will open and main window close.

    We use Internet explorer.

    Thanks in advance.

    ReplyDelete
  7. Great it worked for me too.

    ReplyDelete
  8. Hi all,

    If i want to switch to more than 4 child windows continuously, what will be the code by using enhanced for loop and while (it.hasNext()), Kindly help me out for this scenario

    ReplyDelete
  9. Am not able to switch the control to new window. help me to switch the control

    ReplyDelete