温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

selenium如何打开 IE11浏览器

发布时间:2021-12-18 10:47:49 来源:亿速云 阅读:449 作者:小新 栏目:大数据
# Selenium如何打开IE11浏览器 ## 前言 在Web自动化测试中,Selenium是最常用的工具之一。虽然现代浏览器如Chrome和Firefox更受欢迎,但在某些企业环境中仍需要支持Internet Explorer 11(IE11)。本文将详细介绍如何配置Selenium以启动IE11浏览器,并解决常见问题。 ## 环境准备 ### 1. 安装Selenium WebDriver 首先确保已安装Selenium的Python包: ```bash pip install selenium 

2. 下载IEDriverServer

Selenium官方下载页面获取与你的系统匹配的版本: - 32位和64位版本需与IE安装版本一致 - 推荐下载最新稳定版(如3.141.59)

将下载的IEDriverServer.exe放在: - Python脚本同级目录,或 - 添加到系统PATH环境变量中

基础启动代码

from selenium import webdriver # 基本配置 driver = webdriver.Ie(executable_path='IEDriverServer.exe') driver.get("https://www.baidu.com") driver.quit() 

关键配置参数

1. 忽略缩放设置(必须)

IE对屏幕缩放敏感,需添加:

from selenium.webdriver.ie.options import Options options = Options() options.ignore_zoom_level = True driver = webdriver.Ie(options=options) 

2. 保护模式设置

如果出现保护模式错误:

options = Options() options.require_window_focus = True options.introduce_flakiness_by_ignoring_security_domains = True 

3. 浏览器启动参数

可通过add_argument()添加:

options.add_argument('--ie-use-per-process-proxy') options.add_argument('--ie-force-create-process-api') 

常见问题解决

问题1:初始页面加载失败

现象This is the initial start page for the WebDriver server 解决方案

options = Options() options.initial_browser_url = 'https://www.baidu.com' 

问题2:证书错误

添加忽略SSL错误配置:

options.add_argument('--ignore-certificate-errors') 

问题3:浏览器缩放非100%

需手动确保: 1. 关闭所有IE窗口 2. 设置系统显示缩放为100% 3. 在IE设置中禁用自动缩放

完整示例代码

from selenium import webdriver from selenium.webdriver.ie.options import Options def open_ie11(): options = Options() options.ignore_zoom_level = True options.require_window_focus = True options.add_argument('--ie-use-per-process-proxy') driver = webdriver.Ie( executable_path='IEDriverServer.exe', options=options ) driver.get("https://www.baidu.com") return driver if __name__ == '__main__': driver = open_ie11() print("IE11成功打开!") driver.quit() 

注意事项

  1. UAC提示:以管理员身份运行脚本
  2. 浏览器版本:确保IE11已更新至最新补丁
  3. 注册表设置:某些企业环境需要修改:
     HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main 新建DWORD值:TabProcGrowth = 0 
  4. 组策略限制:检查是否禁用了自动化控制

结论

虽然IE11逐渐退出历史舞台,但在遗留系统测试中仍是必要技能。通过合理配置IEDriverServer和选项参数,可以稳定实现自动化操作。建议新项目优先考虑现代浏览器,仅在对旧系统维护时使用IE11方案。

提示:微软已停止IE支持,建议考虑Edge的IE模式作为替代方案。 “`

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI