Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...