Search On Google

Tuesday, February 19, 2019

Selenium multiple tab handling

How to handle multiple tab in selenium?
We have two given method which excecute on driver
we can get current window object and other window object also by these method.
So if we have object for any tab then we can switch between them easily by code.

Example:-

String MainWindow=driver.getWindowHandle();

This code is useful for taking parent window ID.

Set allwindowList = driver.getWindowHandles()

This code give you all window ID list. By iterating above set you will get different window IDs.
You can iterate and switch at any time when required by following
switching code.

                driver.switchTo().window(childwindowID);

Exampe for switching tabs with selenium java code:-

"package pkg1;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class MyClass912_multiple_tab_handling { 
    public static void main(String[] args) {       
   
        System.setProperty(""webdriver.chrome.driver"",""C:\\Users\\Desk-105\\Documents\\SELENIUM\\chromedriver.exe"");   
        WebDriver driver = new ChromeDriver();   

        driver.get("https://www.onlinetutorial.info/p/selenium-tab-handling.html");
     
     
        String MainWindow=driver.getWindowHandle();
     
     WebElement element =driver.findElement(By.linkText(""Link-a""));
        element.click();

        WebElement element2 =driver.findElement(By.linkText(""Link-b""));
        element2.click();
     
        WebElement element3 =driver.findElement(By.linkText(""Link-c""));
        element3.click();
     
        WebElement element4 =driver.findElement(By.linkText(""Link-d""));
        element4.click();
     
        WebElement element5 =driver.findElement(By.linkText(""Link-e""));
        element5.click();
     
        WebElement element6 =driver.findElement(By.linkText(""Link-f""));
        element6.click();
     
        WebElement element7 =driver.findElement(By.linkText(""Link-g""));
        element7.click();
     
     
     
        /**
         * Tab switching
         */
        for(String handle : driver.getWindowHandles()) {
            if (!handle.equals(MainWindow)) {
             try {
     Thread.sleep(1000);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
                driver.switchTo().window(handle);
                //driver.close();
            }
        }

        try {
   Thread.sleep(2000);
    driver.switchTo().window(MainWindow);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     
        /**
         * Tab switching and closing new tabs
         */
        for(String handle : driver.getWindowHandles()) {
            if (!handle.equals(MainWindow)) {
             try {
     Thread.sleep(1000);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
                driver.switchTo().window(handle);
                driver.close();
            }
        }
     
     
    }
}
"

No comments:

Post a Comment

About Me

My photo
Mumbai, Maharashtra, India