Reputation: 169
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 –
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.
Upvotes: 1
Views: 8783
Reputation: 1
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();
}
}
Upvotes: 0
Reputation: 1956
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.
Upvotes: 0
Reputation: 545
Can you tell if clicking on the link is opening a new tab or a new window?
There is a difference between the link opening in a new window and it opening in a new tab.
Case 1:
In case there are multiple windows, driver.getWindowHandles() returns the list of the handles and hence the size of the list is > 1. Below code demonstrates switching between windows:
//Get the current window handle
String windowHandle = driver.getWindowHandle();
//Get the list of window handles
ArrayList tabs = new ArrayList (driver.getWindowHandles());
System.out.println(tabs.size());
//Use the list of window handles to switch between windows
driver.switchTo().window(tabs.get(0));
//Switch back to original window
driver.switchTo().window(mainWindowHandle);
Case 2:
In case there are multiple tabs in the same window, then there is only one window handle. Hence switching between window handles keeps the control in the same tab.
In this case using Ctrl + \t (Ctrl + Tab) to switch between tabs, has worked for me.
//Open a new tab using Ctrl + t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//Switch between tabs using Ctrl + \t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
Detailed sample code to switch between tabs in same browser window can be found here:
http://design-interviews.blogspot.com/2014/11/switching-between-tabs-in-same-browser-window.html
Upvotes: -3