0

getwindowhandle(); -> Which is used for getting the parent window name.

getwindowhandles(): -> which is used for getting the all the child windows link .

but how to use with string & iterator concept ? for child windows

2

4 Answers 4

0

You can use below code-

String mainwindow=driver.getWindowHandle(); //get parent(current) window name
    for(String popup :driver.getWindowHandles()) //iterating on child windows
    {
    driver.switchTo().window(popup);
    System.out.println("Popup values: "+popup);
    // Perform any operations on child window
    }
    driver.switchTo().window(mainwindow); //switch back to main window & continue further execution
Sign up to request clarification or add additional context in comments.

2 Comments

Ok ... if i want to work with the Child window (newly opened windows) , but that child window name is same as parent window name , how i can use the handles ?
Check if child windows have url. if(driver.getCurrenturl().equals("url of window that you want")){ perform operation }. Otherwise try gettitle.
0

Try this.

 public void SwitchToAnotherWindow(Webdriver driver,int window_number){

     List<String> windowlist = null;

    Set<String> windows = driver.getWindowHandles();

    windowlist = new ArrayList<String>(windows);

   String currentWindow = driver.getWindowHandle();

 if (!currentWindow.equalsIgnoreCase(windowlist.get(window_number - 1))) 
    {
        driver.switchTo().window(windowlist.get(window_number - 1));
     }

}

just pass window number which window you want execute for example if you want switch to 3 window just pass 3 to this method,it can directly shuffle to any window (for ex:1 to 9 window,or 9 to 4 window )

Comments

0
public static void main(String[] args) throws InterruptedException
  {

   WebDriver d = new FirefoxDriver();
   d.get("url");
   String prntwin = d.getWindowHandle();
   System.out.println("Parent window's handle -> " + prntwin);
   WebElement clkElement = d.findElement(By.id("button1")); 

  for(int i = 0; i < 3; i++)
    {
    clkElement.click();
    Thread.sleep(3000);
    }

    Set<String> allWindowHandles = d.getWindowHandles();

   for(String handle : allWindowHandles)
      {
      System.out.println("Window handle - > " + handle);
   }

}

Comments

0
public static void main(String[] args) throws InterruptedException {
    ChromeOptions chromeOption = new ChromeOptions();
    ChromeDriverManager.chromedriver().setup();
    ChromeDriver driver = new ChromeDriver(chromeOption);
    driver.get("https://the-internet.herokuapp.com/");
    driver.manage().window().maximize();
    driver.findElement(By.xpath("//a[contains(text(),'Multiple Windows')]")).click();
    driver.findElement(By.xpath("//a[@target='_blank']")).click();
    Set<String>id=driver.getWindowHandles();
    Iterator<String> ids=id.iterator();
    String Parent=ids.next();
    String Child=ids.next();
    driver.switchTo().window(Child);
    System.out.println(driver.getTitle());
    driver.switchTo().window(Parent);
    System.out.println(driver.getTitle());
}

}

1 Comment

Thank you for your contribution to the community. This may be a correct answer, but it’d be useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who may not be familiar with the syntax. Further, it can help reduce the need for follow-up questions. Would you mind updating your comment with additional details?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.