简体   繁体   中英

Selenium IE driver returns tab count as 1 while using getWindowHandles method on multiple tabs of same window

I'm automating a portal using selenium web driver + Internet Explorer, was able to open a multiple new tabs on existing window but was not able to pass URL to newly opened tab, IE isn't recognizing new tabs and getWindowHandles() returns count as 1, the same code works fine with Chrome.

Sample Code 1 :

String baseUrl = "www.facebook.com";
driver.get(baseUrl);
driver.findElement(By.xpath("//*[@id='userName']")).sendKeys("xyz");
driver.findElement(By.xpath("//*[@id='password']")).sendKeys("****");
driver.findElement(By.xpath("//*[@id='loginButton']")).click();

driver.findElement(By.cssSelector("Body")).sendKeys(Keys.CONTROL +"t");   
driver.getWindowHandles();
ArrayList<String> tabs =  new ArrayList<String>(driver.getWindowHandles()); 

driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.get("www.google.com");

Sample Code 2 :

String baseUrl = "www.facebook.com";
driver.get(baseUrl);
driver.findElement(By.xpath("//*[@id='userName']")).sendKeys("xyz");
driver.findElement(By.xpath("//*[@id='password']")).sendKeys("****");
driver.findElement(By.xpath("//*[@id='loginButton']")).click();

String parent = driver.getWindowHandle();
driver.findElement(By.cssSelector("Body")).sendKeys(Keys.CONTROL +"t");
Thread.sleep(3000);
for(String winHandle : driver.getWindowHandles()){
    if(!winHandle.equals(parent)){
        driver.switchTo().window(winHandle);
        driver.get("www.google.com");
    }
}

Also tried several other ways changing IE configuration like changing protected modes/Overriding automatic cookie handling but nothing worked.

Is there a way to open new tab in IE via code and get the accurate tab count using any method in Selenium?

Configuration:

IE :11, Selenium-java : 2.47/3.7 IEDriver : 3.3.0/2.53, Windows 7

I had a similar problem with IE8 and I solved using driver.switchTo().defaultContent() before call the driver.getWindowHandles(). Something like this:

// Save the parent
parent = driver.getWindowHandle();
driver.switchTo().defaultContent();
for (String winHandle : driver.getWindowHandles()) {
        driver.switchTo().window(winHandle);
        if (!winHandle.equals(parent)) {
        // Do something
        }
}
driver.close();
driver.switchTo().window(parent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM