'Selenium - getWindowHandles() is returning value 1 irrespective of number of browser opened

I am currently automating an application based on IBM Cognos platform using selenium + Cucumber + Internet Explorer and is facing some challenges. I need some expert help from this group on the issues.

Problem Description - Upon clicking a link on the page, new browser get opened. Ideally, getWindowHandles() method returns correct count of browser opened by WebDriver. We switch to 2nd browser using window handle property, further action will be performed on second browser. GetWindowHandles is returning value 1 irrespective of number of browser opened by WebDriver. I have tried following methods –

  1. Click() on the link & tried getWindowHandles()
  2. Keys.Enter on the link & getWindowHandles()
  3. sendkeys(“\0”) on the link & getWindowHandles()
  4. MouseHover on the link, click on the link & getWindowHandles
  5. Double click on the link & getWindowHandles
  6. Opened New tab using sendkeys(keys.control +”t”), navigated to URL & getWindowHandles()
  7. Opened new browser window using sendkeys(keys.control +”n”), navigated to URL & getWindowHandles()

Environment used – Selenium WebDriver – 2.39.0 IEDriverServer.exe - Win32_2.39.0 Windows 7 Internet Explorer 8.0

Note – it is working fine on Firefox, this issue is happening on IE only.



Solution 1:[1]

I was facing the same problem, But adding below capability resolved my issue.

ie.forceCreateProcessApi 

Above capability needs to be added into InternetExplorer. Hope it may resolve your issue too. You may also need to modify reg editor in order to make this working.

Solution 2:[2]

When you access parent window then you should provide some delay. This was my mistake due to which script was not running.You can try this code.

driver.findElement(By.xpath("//a[text()='Multi-PopUp Test']")).click();
String parent=driver.getWindowHandle();
Thread.sleep(3000);
System.out.println("parent window: "+parent);
Set<String>allwindow=driver.getWindowHandles();
int count=allwindow.size();
System.out.println("Total windows: "+count);
for(String child:allwindow)
{
    if(!parent.equalsIgnoreCase(child))
    {
        driver.switchTo().window(child);
        System.out.println("Child window title is:"+driver.getTitle());
        Thread.sleep(3000);
        driver.close();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rupesh Shinde
Solution 2 greg-449