-1

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

whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46
  • Possible duplicate of [How can selenium web driver get to know when the new window has opened and then resume its execution](https://stackoverflow.com/questions/9188343/how-can-selenium-web-driver-get-to-know-when-the-new-window-has-opened-and-then) – JeffC Dec 01 '17 at 19:52
  • The IE driver cannot connect to and control “manually” opened new tabs (i.e., those opened via `Ctrl+t`). There is no workaround. Do not attempt to use tabs with the IE driver. – JimEvans Dec 01 '17 at 19:56
  • @JimEvans : So, When I have to open a new tab and pass a url on a existing execution window, switch to a different browser instead of using IE for such kind of testing - Is the only option? – whoami - fakeFaceTrueSoul Dec 01 '17 at 21:16
  • What’s the difference between what you’re trying to do and creating another driver instance? Or using another top-level window instead of insisting on a tab? – JimEvans Dec 02 '17 at 02:06
  • @JimEvans : I have a child url which is dependent on parent url, So all I need is when we first open a session(instance) with parent url and log-in into the portal,then the child has to be opened on the same session - this child url will provide the credentials used on parent for login purpose, here I have to cross verify the authentication, if I do a new instance on child it would go to a new session, Is there a way to do so instead of opening a new tab on existing session(Can be said a session management)? – whoami - fakeFaceTrueSoul Dec 04 '17 at 16:57
  • How does your site handle sessions? Most do so via a cookie, which you should be able to replicate in the new browser instance. – JimEvans Dec 04 '17 at 21:06
  • @JimEvans : Looking in that direction, Thanks for the help !! – whoami - fakeFaceTrueSoul Dec 04 '17 at 21:18

1 Answers1

0

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);
j.barrio
  • 1,006
  • 1
  • 13
  • 37