19

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?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
Mark Micallef
  • 1,051
  • 2
  • 12
  • 25
  • Lots of folks are suggesting that you ignore this check using DesiredCapabilities, but be careful of this: the zoom level changes where elements are on the page and the IEDriver can't determine where to click based on the zoom offset. – Dave Teply Feb 28 '17 at 23:16

13 Answers13

24

According to the answer given by Jim Evans (one of Selenium developers) in this thread at WebDriver User Group the code below should fix your problem.

DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability("ignoreZoomSetting", true);
driver = new InternetExplorerDriver(caps);
JacekM
  • 4,041
  • 1
  • 27
  • 34
  • Thanks Jacek. Yes, I'd seen that but I'm having trouble doing it with Ruby bindings. – Mark Micallef Aug 21 '12 at 14:08
  • I know this one is a late comment. But, i just want to post it to avoid the confusion for future readers. According to the Jack answer the `ignoreZoomSetting` is to avoid the `Browser Zoom Level` and as per the document too. But, the case is not yet worked in the IEDriverServer.exe. Please refer the chat discussion [here](https://groups.google.com/forum/?fromgroups=#!topic/selenium-users/NXYlqQIAofo). – Manigandan Feb 18 '13 at 04:37
19

Since the question isn't tagged with a specific language, and since JacekM's answer didn't work for me in C# (given the casing, I assume his is for Java...). I'll put the corresponding solution for C# here:

var service = InternetExplorerDriverService.CreateDefaultService(@"Path\To\Driver");
// properties on the service can be used to e.g. hide the command prompt

var options = new InternetExplorerOptions
{
    IgnoreZoomLevel = true
};
var ie = new InternetExplorerDriver(service, options);
Community
  • 1
  • 1
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • This worked for me in so far that it was ignoring the zoom level and running the test. Unfortunately the test fails as it cannot find the elements on the page when the zoom is at 125%. I need to find a way to actually set the zoom to 100%. Any ideas? – dmeehan May 06 '15 at 10:16
  • Open IE browser. And click View. Adjust zoom to 100%. Now all the locators should fit in 100% view. And This solution worked for me. – A user Oct 17 '16 at 14:44
11

adjust browser zoom to 100% To quickly fix it adjust your browser zoom to 100%.

sandeep talabathula
  • 3,238
  • 3
  • 29
  • 38
8

The most robust approach

Before you start with Internet Explorer and Selenium Webdriver Consider these two important rules.

  1. The zoom level :Should be set to default (100%) and
  2. The security zone settings : Should be same for all. The security settings should be set according to your organisation permissions.

How to set this?

Simply go to Internet explorer, do both the stuffs manually. Thats it. No secret.

Do it through your code.

Method 1:

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();

capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);

System.setProperty("webdriver.ie.driver","D:\\IEDriverServer_Win32_2.33.0\\IEDriverServer.exe");

WebDriver driver= new InternetExplorerDriver(capabilities);


driver.get(baseURl);

//Identify your elements and go ahead testing...

This will definetly not show any error and browser will open and also will navigate to the URL.

BUT This will not identify any element and hence you can not proceed.

Why? Because we have simly suppressed the error and asked IE to open and get that URL. However Selenium will identify elements only if the browser zoom is 100% ie. default. So the final code would be

Method 2 The robust and full proof way:

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();

capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);

System.setProperty("webdriver.ie.driver","D:\\IEDriverServer_Win32_2.33.0\\IEDriverServer.exe");

WebDriver driver= new InternetExplorerDriver(capabilities);


driver.get(baseURl);

driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL,"0"));
//This is to set the zoom to default value
//Identify your elements and go ahead testing...

Hope this helps. Do let me know if further information is required.

NiNa
  • 111
  • 1
  • 4
7

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.

LayerCake
  • 169
  • 1
  • 5
4
InternetExplorerOptions options = new InternetExplorerOptions();
options.ignoreZoomSettings() ;
driver = new RemoteWebDriver(new URL("http://localhost:8888/wd/hub"),options);
Martin
  • 2,411
  • 11
  • 28
  • 30
Belnex
  • 41
  • 2
2

This basically happens when your browser is set to some zoom level other than 100%(Happens when you scroll mouse on a web page while pressing the Ctrl key.). You can fix this by specifying the code mentioned above to let selenium to ignore the browser zoom level or you can simply open the browser and reset the zoom level to 100% either by going to settings or using a shortcut Ctrl+0(This worked for IE11 and chrome)

2

Thanks for the post, this really worked for me. For fixing the zoom level exception:

InternetExplorerOptions options = new InternetExplorerOptions {  IgnoreZoomLevel= true };
driver = new InternetExplorerDriver(@"C:\seleniumreferences\IEDriverServer32", options);
DimaSan
  • 12,264
  • 11
  • 65
  • 75
S Kotra
  • 81
  • 2
1

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

JeffG
  • 11
  • 1
0
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.IgnoreZoomLevel = true;
driver = new InternetExplorerDriver(driverFilePath, ieOptions);
duplode
  • 33,731
  • 7
  • 79
  • 150
William
  • 491
  • 5
  • 9
0

Set the IgnoreZoomLevel property to true and pass it as InternetExplorerOptions to the driver.

InternetExplorerOptions options = new InternetExplorerOptions();
options.IgnoreZoomLevel = true;
IWebDriver driver = new InternetExplorerDriver(IEDriverLocation,options);
  • 1
    Please refrain from posting plain code and add some explanatory text. Plain code can be difficult to understand and might attract downvotes due to that. – Adriaan Oct 13 '15 at 21:33
0

As Tomas Lycken's answer said, there is no language specified, so I will share my solution in Python:

capabilities = DesiredCapabilities.INTERNETEXPLORER
capabilities['ignoreZoomSetting'] = True
driver = webdriver.Ie(capabilities=capabilities)
Community
  • 1
  • 1
Pivoman
  • 4,435
  • 5
  • 18
  • 30
0

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();
Léa Gris
  • 17,497
  • 4
  • 32
  • 41
Champ-Java
  • 111
  • 2
  • 13