InternetExplorerDriver Zoom Level Error

Viewed 50703

I'm trying to run tests against IE8 but I've encountered a strange issue:

  1. When creating the webdriver instance (driver = Selenium::WebDriver.for :ie), IE starts up and an exception is thrown by WebDriver:

    "Unexpected error launching Internet Explorer. Browser zoom level was set to 0%"

  2. IE seems to show a failure to connect to the IE Driver Server but if I refresh the browser manually, it connects just fine.

    I have checked online and only two other people seem to have reported this. One possible solution was to ensure that all zones have the same "protected mode" settings, which they do.

    My environment is Windows 7 and IE8 with IE Driver Server v2.25.3 and I'm using the Ruby bindings.

Any ideas?

13 Answers

While setting the IgnoreZoomLevel property allows you to open the browser without error, the test will find no elements at a zoom level other than 100%.

Sending Ctrl+0 will also not always have the expected result, depending on your systems DPI setting. If you have selected Medium (120 dpi) or Larger (144 dpi) (Windows 7 settings) Ctrl+0 will set the zoom to 125% or 150%.

A workaround I found is to set the zoom level according to the DPI settings by editing the setting, before opening IE, in the registry. This does not require administrator rights since everything is located under HKEY_CURRENT_USER.

This is my little helper class I came up with. (C#)

using Microsoft.Win32;

namespace WebAutomation.Helper
{
    public static class InternetExplorerHelper
    {
        private static int m_PreviousZoomFactor = 0;

        public static void SetZoom100()
        {
            // Get DPI setting.
            RegistryKey dpiRegistryKey = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop\\WindowMetrics");
            int dpi = (int)dpiRegistryKey.GetValue("AppliedDPI");
            // 96 DPI / Smaller / 100%
            int zoomFactor100Percent = 100000;
            switch (dpi)
            {
                case 120: // Medium / 125%
                    zoomFactor100Percent = 80000;
                    break;
                case 144: // Larger / 150%
                    zoomFactor100Percent = 66667;
                    break;
            }
            // Get IE zoom.
            RegistryKey zoomRegistryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Internet Explorer\\Zoom", true);
            int currentZoomFactor = (int)zoomRegistryKey.GetValue("ZoomFactor");
            if (currentZoomFactor != zoomFactor100Percent)
            {
                // Set IE zoom and remember the previous value.
                zoomRegistryKey.SetValue("ZoomFactor", zoomFactor100Percent, RegistryValueKind.DWord);
                m_PreviousZoomFactor = currentZoomFactor;
            }
        }

        public static void ResetZoom()
        {
            if (m_PreviousZoomFactor > 0)
            {
                // Reapply the previous value.
                RegistryKey zoomRegistryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Internet Explorer\\Zoom", true);
                zoomRegistryKey.SetValue("ZoomFactor", m_PreviousZoomFactor, RegistryValueKind.DWord);
            }
        }
    }
}

I came up with the values comparing the ZoomFactor value in the registry at different system DPI settings with IE zoom set to 100%. There are more than 3 DPI settings in newer Windows versions, so you need to extend the class if you need those.

You could also modify this to calculate any zoom level you want but that was just not relevant for me.

I just call InternetExplorerHelper.SetZoom100(); before opening IE and InternetExplorerHelper.ResetZoom() after closing it.

InternetExplorerOptions options = new InternetExplorerOptions();
options.ignoreZoomSettings() ;
driver = new RemoteWebDriver(new URL("http://localhost:8888/wd/hub"),options);

Or Goto Internet Explorer Options > Advanced Check the box for “Reset zoom level for new windows and tabs”.

Click Link to see the image ---> Internet Explorer Options > Advanced

Working Code using Java

InternetExplorerOptions capabilities= new InternetExplorerOptions();
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
System.setProperty("webdriver.ie.driver", Constant.drivers + "\\IEDriverServer.exe");
            driver = new InternetExplorerDriver(capabilities);
            driver.manage().window().maximize();
Related