1

Description:

I know how to switch back and forth from main window to child window and vice versa. But for some reason the same code doesn't seems to be working on Internet Explorer 11.

I have searched Google, and my issue, but my case is a bit peculiar because I am able to switch from main window to the child window. But once I close the child window I am not able to switch back to the main window.

Steps in brief:

  1. Open a url in Internet Explorer 11.
  2. Click on a link, It opens a new Child window.
  3. Do some actions on the window and close the Child Window.
  4. Switch back to the main window. - This step is not getting executed and throwing an error of time out Exception.

Below is my Code:

public static void main(String[] args) {

    InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();
    internetExplorerOptions.ignoreZoomSettings();
    internetExplorerOptions.introduceFlakinessByIgnoringSecurityDomains();
    internetExplorerOptions.requireWindowFocus();

    System.setProperty("webdriver.ie.driver","path_of_the_IE_Driver/IEDriverServer.exe");
    WebDriver driver = new InternetExplorerDriver(internetExplorerOptions);
    WebDriverWait wait = new WebDriverWait(driver, 30);

    driver.get("parent_Window.html");

    //Getting the main window handle
    String mainWindowHandle = driver.getWindowHandle();

    driver.findElement(By.id("button")).click();//opens a new(child) window

    //Switching to the child window
    Set<String> allHandles = driver.getWindowHandles();
    for (String handle : allHandles) {
        String title = driver.switchTo().window(handle).getTitle();

        if (title.equals("Child_Window_Title")) {
            driver.switchTo().window(handle);
            break;
        }
    }

    //Closing the child window
    driver.close();

    //This line not getting executed and throws the Time out Exception
    driver.switchTo().window(mainWindowHandle);

}

2 Answers 2

1

It's been a long time since I've automated IE11, and forgot how much of a pain it is. Make sure you've followed the setup guide located here https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration which says:

The IEDriverServer exectuable must be downloaded and placed in your PATH.

On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings, choose "Internet Options..." from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled "Enable Protected Mode".

Additionally, "Enhanced Protected Mode" must be disabled for IE 10 and higher. This option is found in the Advanced tab of the Internet Options dialog. The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.

For Windows 10, you also need to set "Change the size of text, apps, and other items" to 100% in display settings.

For IE 11 only, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates. For 32-bit Windows installations, the key you must examine in the registry editor is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. For 64-bit Windows installations, the key is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present. Important: Inside this key, create a DWORD value named iexplore.exe with the value of 0.

Without an example site that shows the issue, everyone answering is going to be guessing to some extent.

With that said, I modified your code to work against Google and I've been successful in switching to the new window and back.

package webdriver_example;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.Set;

public class Ie {

    public static void main(String[] args) {

        InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();
        internetExplorerOptions.ignoreZoomSettings();
        internetExplorerOptions.introduceFlakinessByIgnoringSecurityDomains();
        internetExplorerOptions.requireWindowFocus();

        System.setProperty("webdriver.ie.driver","C:/Users/will/Downloads/IEDriverServer_x64_3.14.0/IEDriverServer.exe");
        WebDriver driver = new InternetExplorerDriver(internetExplorerOptions);
        WebDriverWait wait = new WebDriverWait(driver, 30);

        driver.get("http://www.google.co.uk");

        //Getting the main window handle

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String mainWindowHandle = driver.getWindowHandle();

        WebElement link = driver.findElement(By.tagName("a"));

        new Actions(driver)
                .keyDown(Keys.CONTROL)
                .click(link)
                .keyUp(Keys.CONTROL)
                .build()
                .perform();

        new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2));
        Set<String> windowHandles = driver.getWindowHandles();
        System.out.println(windowHandles);

        //Switching to the child window
        Set<String> allHandles = driver.getWindowHandles();
        for (String handle : allHandles) {
            String title = driver.switchTo().window(handle).getTitle();

            System.out.println(title);

            if (title.equals("About | Google")) {
                System.out.println("Actually switching");
                driver.switchTo().window(handle);
                break;
            }
        }

        //Closing the child window
        driver.close();

        //This line not getting executed and throws the Time out Exception
        driver.switchTo().window(mainWindowHandle);

        new Actions(driver)
                .keyDown(Keys.CONTROL)
                .click(link)
                .keyUp(Keys.CONTROL)
                .build()
                .perform();

    }
}

Note the part where it waits for there to be more than one window before switching. I found with your initial code it wouldn't switch at all, and ended up closing the original window when calling driver.close(), this may explain why you then couldn't locate things on the original window.

If that doesn't fix it, try locating all the window handles again after you close the child window, and switch to the only one that exists. The reference to the main window handle may be becoming "stale" somehow.

2
  • Thanks a lot for your detailed answer. Will surely try and let you know if it worked or not. Cheers.
    – Alok
    Commented Feb 3, 2019 at 7:13
  • Hey @anonygoose I tried the above solution but it didn't work.
    – Alok
    Commented Feb 7, 2019 at 3:32
0

The official selenium documentation suggests that you need to both:

1. Have your internet explorer's zoom level set to 100% and enter image description here

2. If you're running windows 10, you need to "Change the size of text, apps, and other items" in "Display Settings" to 100%. (Make sure that you're doing this action on the monitor that you're running tests on.) enter image description here

You can also find a relevant thread on this issue here.

other possible solutions from that thread include setting the following settings:

        var options = new InternetExplorerOptions();
        options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
        options.IgnoreZoomLevel = true;
        options.UnhandledPromptBehavior = UnhandledPromptBehavior.Accept;
        options.EnablePersistentHover = true;
        options.EnableNativeEvents = false;
        options.EnsureCleanSession = true;   
        options.ElementScrollBehavior = InternetExplorerElementScrollBehavior.Top;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.