Using getwindowhandle() and getWindowHandles() methods in Selenium WebDriver

getwindowhandle() and getWindowHandles() in selenium are methods to get window handles in selenium. getwindowhandle() is useful to get handle of current selected window in selenium while getWindowHandles() will return handles of all open windows or tabs. Both these methods getwindowhandle() and getWindowHandles() in selenium are useful when you are working with multiple windows and you need to switch from one window to another window.

How to use getwindowhandle() method in selenium?

package TestPack;

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

public class testgetWindowHandle {

 public static void main(String[] args) {
  String exePath = "D:\\chromedriver_win32\\chromedriver.exe";
  System.setProperty("webdriver.chrome.driver", exePath);
  WebDriver driver = new ChromeDriver();
  
  driver.get("https://only-testing-blog.blogspot.com/2014/01/textbox.html");
    
  //Get current browser window handle using getWindowHandle.
  String tabonehandle = driver.getWindowHandle();
  System.out.println(tabonehandle +" is current browser window handle.");
 }
}

Output : CDwindow-8C98EDC035FFE4FD2C644AF7F6FBB0B4 is current browser window handle.

In above example you can see that selenium getwindowhandle() method has retrieved current browser's window handle and print it in console.

Now let's see how getwindowhandles in selenium works to get all window handle.

How to use getWindowHandles() method on selenium?

package TestPack;

import java.util.Iterator;
import java.util.Set;

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

public class testgetWindowHandles {

 public static void main(String[] args) {
  String exePath = "D:\\chromedriver_win32\\chromedriver.exe";
  System.setProperty("webdriver.chrome.driver", exePath);
  WebDriver driver = new ChromeDriver();
  
  driver.get("https://only-testing-blog.blogspot.com/2014/01/textbox.html");
  driver.findElement(By.xpath("//b[contains(text(),'Open New Page')]")).click();
  
  //Get main window handle.
  String tabonehandle = driver.getWindowHandle();
  System.out.println(tabonehandle +" is tab one window handle.");
  
  //Get all windows handles using getWindowHandles() in selenium
  Set<String> allhandles=driver.getWindowHandles();
  
  //Iterate through allhandles.
  Iterator<String> it = allhandles.iterator();
  
  String tabtwohandle = null;
  
      while(it.hasNext()){
       //Store iterator window handle value in temp
       String temp = it.next();
       
       //compare temp value with tabonehandle
      if(!tabonehandle.equalsIgnoreCase(temp)) {
       //If not match with tabonehandle then it is tab two handle and store it in tabtwohandle.
       tabtwohandle = temp;
       System.out.println(tabtwohandle  +" is tab two window handle.");
      }
      
      }
      
      //Switch to tab two of browser using tabtwohandle
      driver.switchTo().window(tabtwohandle);
      //type text in text box of tab two
      driver.findElement(By.id("fname")).sendKeys("some text");
      
     //Switch to tab one of browser using tabonehandle
      driver.switchTo().window(tabonehandle);
      //select radio button on tab one.
      driver.findElement(By.id("radio2")).click();      
 }
}

Result : It will switch between two tabs of browser and perform some actions.

In above example, Initially we stored tabonehandle using getWindowHandle() method. Next we clicked on link to open page in new tab. On this stage, We have two tabs open. Then we got all window handles using getWindowHandles() method and retrieved tabtwohandle. So we have both tabs handle and we can switch between two tabs using driver.switchTo().window(handlename).

When to use close() and quit() methods?

When you need getwindowhandle() and getWindowHandles() in selenium?

You need getwindowhandle() and getWindowHandles() in selenium when you are working on multiple windows or tabs and need to switch between them.

No comments:

Post a Comment