Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Handling Multiple windows in Selenium

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.00k
    Comment on it

    Handling the  multiple windows or window based alert pop ups is beyond web driver capabilities. To handle this we need some third party utilities(alerts API).

     

    • Web driver interface provides two methods to handle this:
    • To access the window pop up,  getWindowHandles() and getWindowHandle()  methods are used.
    • If there are multiple windows are opened by web driver and we need to access all windows we use "Driver.getWindowHandles()", using this method we can switch from one window to another window.

    When we have to handle only main window we used  "Driver.getWindowHandles()" method and it will handle only current window.

    Example:-

    Steps:

    1. Launch the application(http://toolsqa.com/automation-practice-switch-windows/)
    2. Click on 'New browsers window' button, it will open in new window
    3. Maximize the new child window
    4. Verify the text(Selenium Online Training) on new window
    5. Click on 'Enroll Your self' button
    5. close the child window and return to main window.

    Code:

    import java.util.Iterator;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    
    public class Multiplewindowhandling {
    
      public static void main(String[] args) {
      
      
    
      WebDriver driver = new FirefoxDriver();
    
      //launch the application
    
      driver.navigate().to("http://toolsqa.com/automation-practice-switch-windows/");
      driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
     
    
      
      driver.findElement(By.xpath(".//*[@id='button1']")).click();
      
      String MainWindow=driver.getWindowHandle();
      
    
       //To handle all new open window
    
      Set<String> windowids = driver.getWindowHandles();
      Iterator<String> iterate = windowids.iterator();
    
      while(iterate.hasNext())   
            {  
                String ChildWindow=iterate.next();  
                  
                if(!MainWindow.equalsIgnoreCase(ChildWindow))   
                {      
                     
                        // Switching to Child window
                        driver.switchTo().window(ChildWindow);
                        driver.manage().window().maximize();
                                                                                             
                                                                                                 
                    driver.findElement(By.xpath(".//*[@id='rev_slider_8_1']/ul/li[1]/div[2]")).isDisplayed();
      
      driver.findElement(By.xpath(".//*[@id='rev_slider_8_1']/ul/li[1]/div[6]/a")).click();   
                                               
                           
                                    
       // Closing the Child Window.
                            driver.close();  
                }  
            }
      
            // Switching to Parent window i.e Main Window.
                driver.switchTo().window(MainWindow);
    }
    
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: