在 Edge IE 模式下,如何切换至 IEDriver 并处理 SSL 证书?
解读
国内银行、政务、ERP 等老系统仍只认 IE 内核,Edge 的“IE 模式”成了 Win11/Win10 上的唯一合法入口。面试官问“如何切到 IEDriver”并不是让你去装一个独立 IE11,而是考察你能否让 Selenium IEDriverServer.exe 正确挂到 Edge-Chromium 的 IE 模式页签 上,并顺带把 SSL 证书错误页面(证书不信任、过期、自签) 自动点掉,保证脚本在 CI 里 7×24 不卡壳。答不上来就直接被归为“只能测新版 Chrome 的玩具选手”。
知识点
- Edge IE 模式本质:Edge 进程用 ieframe.dll 渲染,但顶层窗口还是 msedge.exe;IEDriver 必须通过 “附加到已有 IE 实例” 的方式接入。
- 匹配版本:IEDriverServer.exe 与 Edge 大版本 必须一致(Win11 22H2 自带 Edge 118,就配 118.x.xxxx.x 的 IEDriver)。
- 必需注册表:
[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main]
"TabProcGrowth"=dword:00000000
否则 Edge 每开一个 IE 模式页签就起新进程,Driver 找不到窗口。 - SSL 证书错误页属于 about:security 系列,地址栏会停在 “javascript:void(0)”,必须先把 “Enable Protected Mode” 四区域统一勾选/去勾选,再让 Driver 点击 “overridelink” 或注入 document.getElementById('overridelink').click()。
- Capability 关键字:
ie.ensureCleanSession=trueie.ignoreProtectedModeSettings=trueacceptInsecureCertificates=true(IEDriver 不认,得靠页面脚本)
- 国内 CI 常见坑:
- Jenkins 以 LocalSystem 安装时无桌面,需把 IEDriver 与 Edge 都设为 Session0 穿透 或改用 Jenkins Agent 以真实用户登录。
- 国产化 Windows 10 政府版自带 国密根证书,需提前导入到 “受信任的根证书颁发机构”,否则脚本点完 override 仍会被 国密校验 弹窗阻断。
答案
-
环境准备
a) 下载与 Edge 大版本一致的 IEDriverServer.exe(32 位即可,64 位切图延迟高),放到 *C:\tools\webdriver* 并加入 系统 PATH。
b) 在 Edge 设置 → 默认浏览器 → 允许在 Internet Explorer 模式下重新加载网站 设为 “允许”,并把被测地址加入 IE 模式策略列表(企业策略或本地 JSON)。
c) 注册表关闭 TabProcGrowth,四区域 保护模式 统一启用或统一禁用,避免 Unexpected error launching Internet Explorer。 -
代码示例(Python,国内镜像源)
from selenium import webdriver
from selenium.webdriver.ie.service import Service
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
ie_options = webdriver.IeOptions()
ie_options.attach_to_edge_chrome = True
ie_options.edge_executable_path = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
ie_options.add_additional_option("ie.ensureCleanSession", True)
ie_options.ignore_protected_mode_settings = True
ie_options.ignore_zoom_level = True
caps = DesiredCapabilities.INTERNETEXPLORER.copy()
caps['acceptInsecureCertificates'] = True # 仅标记,实际仍需脚本点掉
service = Service(executable_path=r'C:\tools\webdriver\IEDriverServer.exe')
driver = webdriver.Ie(service=service, options=ie_options, capabilities=caps)
# 打开站点,Edge 自动走 IE 模式
driver.get("https://your-legacy-bank.gov.cn")
# 处理 SSL 证书错误页
if "Certificate Error" in driver.title or "证书错误" in driver.title:
# 方法一:直接点 override 链接
driver.execute_script("document.getElementById('overridelink').click();")
# 方法二:如果页面被框住,先切到最外层 window
driver.switch_to.default_content()
time.sleep(1)
driver.execute_script("document.getElementById('overridelink').click();")
# 后续正常业务流
driver.find_element("id", "username").send_keys("admin")
- 验证点
- 任务管理器里应只有 msedge.exe 与 IEDriverServer.exe,无 iexplore.exe。
- 地址栏左侧出现 IE 图标 即表示成功进入 IE 模式。
- 在 无头 Jenkins 上跑通,SSL 错误页 不再卡住,截图 能看到登录框。
拓展思考
- 如果企业策略强制 IE 模式使用独立 IE 进程(TabProcGrowth=1),IEDriver 的 “attach to Edge” 会失效,此时只能退而求其次:
a) 用 Selenium 3 的 InternetExplorerDriver 直接启动 iexplore.exe,再通过 企业模式站点列表 把域名硬指到 IE11;
b) 或者升级到 Selenium 4.15+,官方已支持 “edge-in-ie-mode” 的 service port 自动发现,可 动态附加 无需改注册表。 - 国密 SSL 场景下,override 链接 点完后仍可能弹出 “国密证书不信任” 的 WinInet 弹窗,此时需提前把 国密根证书 导入 “第三方根证书存储” 并用 certutil -addstore root xxx.cer 静默安装,否则只能走 AutoIt 或 Windows UIAutomation 硬点。
- 并行执行:IEDriver 默认 单实例,如需并发,可在 Jenkins 多节点 上各起 独立 Windows 会话(mstsc /admin),配合 Selenium Grid 4 的 Node UI 模式,把 Edge IE 模式 注册成 ie 类型浏览器,实现 分布式回归。