getWindowHandle() and getWindowHandles() in java webdriver

Hello,

Could you please let me know what is the use below two methods.

A) . getWindowHandle()
1. What it will return?.
2.If it return any value then how to use it to handle window.

B) .getWindowHandles()
1. What it will return?.
2.If it return any value then how to use it to handle window.
3. How to use the values (For SwitchTo method), when if you have more than 2 pop-up windows.

Thanks,

Doctor Asked on August 22, 2014 in Selenium WebDriver.
Add Comment
  • 4 Answer(s)

    Hi Rocky,

    For both questions the answer is “Yes, it will return the value.”.
    The first command will return a string of alphanumeric window handle and the second command will return a set of window handle. For more details and usage of those commands, visit http://www.toolsqa.com/selenium-webdriver/switch-commands/.

    Regards,
    Vaidy

    Doctor Answered on August 22, 2014.
    Add Comment

    Hi Vaidy,

    I am looking into particular solution, how to switch to a particular window using handle/String values?.

    Below is the sample code:

    
    import java.util.concurrent.TimeUnit;
    
    import org.junit.Test;
    
    import org.openqa.selenium.By;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class Test2 {
    
    @Test
    
    public void test5() throws Exception {
    
    WebDriver driver = new FirefoxDriver();
    
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    
    driver.get("http://site22.way2sms.com/content/index.html");
    String mainwindow=driver.getWindowHandle();
    
    System.out.println("Main window value: "+mainwindow);
    
    driver.findElement(By.linkText("Contact")).click();
    for(String popup :driver.getWindowHandles()){
    
    driver.switchTo().window(popup); // How to switch to these values?.
    
    System.out.println("Popup values: "+popup);
    
    }
    
    driver.switchTo().window(mainwindow);
    
    driver.quit();
    
    }
    
    }
    
    
    Doctor Answered on August 23, 2014.
    Add Comment

    hi rocky

    you can try this if  you wanted to hold up in some window and wanna do some operations there

    package com.problems.toolsqa;

    import java.util.concurrent.TimeUnit;

    import org.junit.Test;

    import org.openqa.selenium.By;

    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class Windowhandlers {

    @Test

    public void test5() throws Exception {

    WebDriver driver = new FirefoxDriver();

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.get(“http://site22.way2sms.com/content/index.html”);
    String mainwindow=driver.getWindowHandle();

    System.out.println(“Main window value: “+mainwindow);

    driver.findElement(By.linkText(“Contact”)).click();
    for(String popup :driver.getWindowHandles()){

    driver.switchTo().window(popup); //<strong>// How to switch to these values?.</strong>

    System.out.println(“Popup values: “+popup);

    System.out.println(“the values of titl” +driver.getTitle());

    if(driver.getTitle().contains(“Contact”))   // this is to make sure that you are on right pop up before performing operation

    {
    // perform some opertraion

    System.out.println(“now this is working”);
    System.out.println(driver.getCurrentUrl());

    String text=    driver.findElement(By.xpath(“//h3[text()=’Advertising Inquiries’]”)).getText();

    System.out.println(text);

    }
    }

    driver.switchTo().window(mainwindow);

    driver.quit();

    }

    }

    and also let me know if you find some better solution

    thanx

    ankit

    Vice Professor Answered on August 23, 2014.
    Add Comment

    Hi Ankit,

    We are on the same boat, but bit difference is there. Please find below code I am also using “Title” to switch. But need to know what is the use of o/p of the getwindowhandle/handles()?, how we can use it in coding?.

    
    if(driver.getTitle().equals("Way2SMS - Contact"))
    
    
    Doctor Answered on August 24, 2014.

    hi rocky

    Set<String> windowHandles = driver.getWindowHandles();
    driver.switchTo().window(windowHandles.toArray()[1].toString());
    String title=driver.getTitle();
    System.out.println(title);

    window handles—> it contains a list of handles

    handles in simple words you can understand as something  that can be used to identified those window

    as  web elements can be indetified by locators

    so in case of windows you can assume a handle as locater if i am not wrong  ( i understand it in this way)

    if you will try printing the values of window handles you will get some alphanumeric values

    and let me knw if its is clear to you else i wiill try some more examples

    on August 24, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.