1

Here's the code, it should print 3 but it prints only 1.

Don't understand whats not working. (Due to this I also not able to switch to new window. I want to switch to window by index.)

driver.get("https://www.google.co.in");

//open two more tabs        
act.keyDown(Keys.LEFT_CONTROL).perform();
driver.findElement(By.linkText("Images")).click();
driver.findElement(By.linkText("Images")).click();

// save windows in a set
Set<String> winlist = driver.getWindowHandles();
Thread.sleep(5000);

//print set size --------> wrong, prints only 1
System.out.println(winlist.size());
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
paul
  • 4,333
  • 16
  • 71
  • 144
  • possible duplicate of ["switchTo" method not working for switching to tabs in browser](http://stackoverflow.com/questions/32422716/switchto-method-not-working-for-switching-to-tabs-in-browser) – JeffC Sep 07 '15 at 07:07

2 Answers2

0

It does seems like an issue with Firefox driver only. I was able to reproduce with current release of Firefox and Selenium version 2.47.1. However, I do believe downgrading the Firefox might solve the issue and I haven't tried that. I did try the same code block on Chrome just to make sure the issue is driver related and had expected result.

public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver", "E:\\working\\selenium\\drivers\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    Actions act = new Actions(driver);
    driver.get("https://www.google.co.in");

    //get the original window handles
    String originalHandle = driver.getWindowHandle();
    //open two more tabs        
    act.keyDown(Keys.LEFT_CONTROL).perform();
    driver.findElement(By.linkText("Images")).click();
    driver.findElement(By.linkText("Images")).click();

    // save windows in a set
    Set<String> winlist = driver.getWindowHandles();
    for(String win:winlist){
        if(!win.equals(originalHandle)){
            driver.switchTo().window(win);
            System.out.println(driver.getTitle());
        }
    }    
    //print set size --------> wrong, prints only 1
    System.out.println(winlist.size());
}

Console Output:

Starting ChromeDriver 2.16.333243

(0bfa1d3575fc1044244f21ddb82bf870944ef961) on port 25687

Only local connections are allowed.

Google Images

Google Images

3

Saifur
  • 16,081
  • 6
  • 49
  • 73
0

What helps me to fix this issue: the problem was in declaration of ChromeDriver: change "WebDriver driver = new ChromeDriver();" to "ChromeDriver driver = new ChromeDriver();"

So ChromeDriver creation should look like:

System.setProperty("webdriver.chrome.driver", "path to chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in");
Olga
  • 1