0
package demo.webtables;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class WebTablesAssignment03 {

    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("file:///C:/Users/Chandra/Desktop/QA/Assignment-3.html");
        String parent = driver.getWindowHandle();
        System.out.println(parent);
        Actions action = new Actions(driver);

        int columncount = driver.findElements(By.xpath("//tbody/tr[1]/td")).size();
        for (int i = 1; i <= columncount; i++) {
            WebElement element = driver.findElement(By.xpath("//tbody/tr[" + i + "]/td[3]/a/div"));
            action.contextClick(element).perform();
            action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        }
        Set<String> allTabs = new HashSet<>();
        Set<String> allWindows = driver.getWindowHandles();
        System.out.println(allWindows);
        for (String child : allWindows) {
            if (!parent.equalsIgnoreCase(child)) {
                driver.switchTo().window(child);
                allTabs.add(driver.getTitle());
                driver.switchTo().window(parent);
            }
            System.out.println(allTabs);
        }

    }

}

Using selenium version 2.53 and firefox 46

4 tabs opened but the set remains empty and the window id is only one

The output is :

{aabd13c1-df99-4e3a-8631-b7fe681642e8}
[{aabd13c1-df99-4e3a-8631-b7fe681642e8}]
[]

here is the link to the html table in driver.get(); https://drive.google.com/file/d/15FaqvibskNB2FjCUyNuYI6wC3fJvDAmh/view?usp=sharing

same issue with following code:

import java.util.ArrayList;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TabSwitching1 {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        //System.setProperty("webdriver.chrome.driver", "C:\\Users\\Chandra\\Desktop\\SELENIUM\\Chromedriver\\chromedriver.exe");
        //WebDriver driver = new ChromeDriver();
        driver.get("C:\\Users\\Chandra\\Desktop\\QA\\Assignment-3.html");
        String windowHandle = driver.getWindowHandle();
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"t");
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"t");
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"t");
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"t");
        ArrayList <String>tabs = new ArrayList(driver.getWindowHandles());
        System.out.println(tabs);
        System.out.println(tabs.size());
        driver.switchTo().window(tabs.get(0)); 

    }

}
0

Before you try to switch to another window, check, if window size is greater than 1, so that you can switch to newly opened window, Also use of Hash Map is unnecessary here, Please try below code.Pass your web-driver to the method and run this code.

 public static void switchWindow(WebDriver driver) throws InterruptedException {
    String cHandle = driver.getWindowHandle();
    String newWindowHandle = null;
    Set<String> allWindowHandles = driver.getWindowHandles();
    for (int i = 0; i < 20; i++) {
      if (allWindowHandles.size() > 1) {
        for (String allHandlers : allWindowHandles) {
          if (!allHandlers.equals(cHandle))
            newWindowHandle = allHandlers;
        }
        driver.switchTo().window(newWindowHandle);
        break;
      } 
    }
    if (cHandle == newWindowHandle) {
      throw new RuntimeException("Time out - No window found");
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.