InternetExplorerDriver缩放级别错误

时间:2012-08-20 09:12:46

标签: selenium webdriver selenium-webdriver

我正在尝试对IE8进行测试,但我遇到了一个奇怪的问题:

  1. 创建webdriver实例(driver = Selenium :: WebDriver.for:ie)时,IE启动并WebDriver抛出异常:

    “启动Internet Explorer时出现意外错误。浏览器缩放级别设置为0%”

  2. IE似乎显示无法连接到IE驱动程序服务器,但如果我手动刷新浏览器,它连接就好了。

    我在网上查了一下,其他两个人似乎都报了这个。一种可能的解决方案是确保所有区域都具有相同的“保护模式”设置。

    我的环境是带有IE Driver Server v2.25.3的Windows 7和IE8,我正在使用Ruby绑定。

  3. 有什么想法吗?

13 个答案:

答案 0 :(得分:21)

根据Jim Evans(Selenium开发人员之一)在this thread at WebDriver User Group中给出的答案,下面的代码应该可以解决您的问题。

DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability("ignoreZoomSetting", true);
driver = new InternetExplorerDriver(caps);

答案 1 :(得分:13)

由于问题没有用特定语言标记,并且因为JacekM's answer在C#中对我不起作用(考虑到外壳,我认为他的是用于Java ...)。我将在这里为C#提供相应的解决方案:

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);

答案 2 :(得分:10)

adjust browser zoom to 100% 要快速修复它,请将浏览器缩放调整为100%。

答案 3 :(得分:7)

最强大的方法

在开始使用Internet Explorer和Selenium Webdriver之前请考虑以下两个重要规则。

  1. 缩放级别:应设置为默认值(100%)和
  2. 安全区域设置:对所有人都应该相同。应根据您的组织权限设置安全设置。
  3. 如何设置?

    只需转到Internet Explorer,手动执行这两项操作。而已。不是秘密。

    通过您的代码完成。

    方法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...
    

    这绝对是不显示任何错误,浏览器将会打开,并且还会导航到该网址。

    不会识别任何元素,因此您无法继续。

    为什么呢?因为我们已经模拟了这个错误,并要求IE打开并获取该URL。但是 Selenium仅在浏览器缩放为100%时识别元素。默认即可。所以最终的代码是

    方法2强大且完整的证明方式:

    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...
    

    希望这会有所帮助。如果需要进一步的信息,请告诉我。

答案 4 :(得分:6)

设置IgnoreZoomLevel属性允许您无错误地打开浏览器,测试将找不到100%以外的缩放级别的元素。

发送Ctrl + 0也不会始终具有预期结果,具体取决于您的系统DPI设置。如果您选择了中(120 dpi)或更大(144 dpi)(Windows 7设置),Ctrl + 0会将缩放设置为125%或150%。

我找到的解决方法是通过在注册表中打开IE之前编辑设置,根据DPI设置设置缩放级别。这不需要管理员权限,因为所有内容都位于HKEY_CURRENT_USER下。

这是我提出的小助手课程。 (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);
            }
        }
    }
}

我想出了在不同系统DPI设置下比较注册表中ZoomFactor值并将IE缩放设置为100%的值。在较新的Windows版本中有超过3个DPI设置,因此如果需要,您需要扩展该类。

你也可以修改它来计算你想要的任何缩放级别,但这与我无关。

我打开IE之前只调用InternetExplorerHelper.SetZoom100();,关闭之后调用InternetExplorerHelper.ResetZoom()

答案 5 :(得分:2)

当您的浏览器设置为100%以外的某个缩放级别时,基本上会发生这种情况(在按住Ctrl键的同时在网页上滚动鼠标时会发生这种情况。)。您可以通过指定上面提到的代码来解决这个问题,让selenium忽略浏览器缩放级别,或者只需打开浏览器并通过转到设置或使用快捷键Ctrl + 0将缩放级别重置为100%(这适用于IE11和chrome)

答案 6 :(得分:2)

感谢帖子,这对我来说真的很有用。 用于修复缩放级别异常:

InternetExplorerOptions options = new InternetExplorerOptions {  IgnoreZoomLevel= true };
driver = new InternetExplorerDriver(@"C:\seleniumreferences\IEDriverServer32", options);

答案 7 :(得分:2)

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

答案 8 :(得分:1)

或转到Internet Explorer选项>高级 选中“重置新窗口和标签的缩放级别”框。

点击链接查看图片---> Internet Explorer Options > Advanced

答案 9 :(得分:0)

InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.IgnoreZoomLevel = true;
driver = new InternetExplorerDriver(driverFilePath, ieOptions);

答案 10 :(得分:0)

将IgnoreZoomLevel属性设置为true,并将其作为InternetExplorerOptions传递给驱动程序。

InternetExplorerOptions options = new InternetExplorerOptions();
options.IgnoreZoomLevel = true;
IWebDriver driver = new InternetExplorerDriver(IEDriverLocation,options);

答案 11 :(得分:0)

正如Tomas Lycken's answer所说,没有指定语言,所以我将在 Python 中分享我的解决方案:

capabilities = DesiredCapabilities.INTERNETEXPLORER
capabilities['ignoreZoomSetting'] = True
driver = webdriver.Ie(capabilities=capabilities)

答案 12 :(得分:0)

使用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();