InternetExplorerDriver缩放级别错误

Mar*_*lef 19 selenium webdriver selenium-webdriver

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

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

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

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

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

    我的环境是Windows 7和IE8与IE驱动程序服务器v2.25.3,我正在使用Ruby绑定.

有任何想法吗?

Jac*_*ekM 24

根据Jim Evans(Selenium开发人员之一)在WebDriver用户组这个主题中给出的答案,下面的代码应该可以解决您的问题.

DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability("ignoreZoomSetting", true);
driver = new InternetExplorerDriver(caps);
Run Code Online (Sandbox Code Playgroud)


Tom*_*han 16

由于问题没有用特定的语言标记,并且因为JacekM的答案在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);
Run Code Online (Sandbox Code Playgroud)


san*_*ula 12

将浏览器缩放调整为100% 要快速修复它,请将浏览器缩放调整为100%.


小智 8

最强大的方法

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

  1. 缩放级别:应设置为默认值(100%)和
  2. 安全区域设置:对所有人来说应该是相同的.应根据组织权限设置安全设置.

怎么设置这个?

只需转到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...
Run Code Online (Sandbox Code Playgroud)

这肯定不会显示任何错误,浏览器将打开,并将导航到URL.

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

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

方法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...
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.如果需要进一步的信息,请告诉我.


小智 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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

您也可以修改它来计算您想要的任何缩放级别,但这对我来说无关紧要.

我只是InternetExplorerHelper.SetZoom100();在打开IE之前和InternetExplorerHelper.ResetZoom()关闭它之前打电话.