综述
webdriver.EdgeOptions(), webdriver.ChromeOptions(), 和 webdriver.IeOptions() 都是 Selenium WebDriver 的设置类,用于定制化启动各自欣赏器的设置。它们分别对应 Microsoft Edge,Google Chrome,和 Internet Explorer 欣赏器。
每个欣赏器的驱动器都大概支持一些特定的选项和特性,但是在操纵上,大部分方法和功能都是相似的,因为它们都遵照雷同的计划原则。以下是一些通常的操纵和区别:
通用操纵:
- 添加新的欣赏器参数(如 --headless 启动欣赏器无头模式)。
- 添加扩展(Extensions)。
- 设定下载文件夹路径。
- 忽略证书错误。
- 设置代理。
特定于 Chrome 和 Edge 的操纵:
由于 Microsoft Edge 基于 Chromium,它和 Chrome 在许多选项上是相似的。这意味着 EdgeOptions 和 ChromeOptions 具有许多共通之处,好比它们都可以:
- 设置用户代理(User-Agent)。
- 设置启动参数(如 --incognito 打开无痕模式)。
- 添加扩展(Extensions)。
- 启用实验性子的特性设置(Experiments)。
特定于 Internet Explorer 的操纵:
- Internet Explorer(IE)驱动器,通过 IeOptions,提供了一些特有的选项和行为控制,例如:
- 忽视保护模式设置(Introduce Instability By Ignoring Protected Mode Settings)。
- 设置初始欣赏器URL(Initial Browser URL)。
- 启用或禁用本地化存储(Enable or Disable Persistent Hovering)。
- IE 保护模式的设置必须在全部地区雷同才能避免权限错误。
虽然基本的设置方法相似,但是因为欣赏器本身的差别,部分特定的选项和行为大概会有所不同。IE 驱动器特殊注意安全性设置,因为 IE 的设置比较复杂,并且它受到 Windows 操纵体系安全模型的影响较大。相比之下,Chrome 和 Edge 因为共享了雷同的基础(Chromium),它们的设置选项更为相似,并且通常也更为现代化。
代码演示
以下是如何分别使用 Selenium WebDriver 的 EdgeOptions, ChromeOptions, 和 IeOptions 来设置相应欣赏器的示例代码。
对于 Microsoft Edge (Chromium):
- from selenium import webdriver
- edge_options = webdriver.EdgeOptions()
- edge_options.use_chromium = True # 指定使用基于Chromium的Edge浏览器
- edge_options.add_argument("headless") # 无界面模式
- edge_options.add_argument("disable-gpu") # 禁用GPU加速
- edge_options.add_argument("window-size=1200x600") # 设置窗口大小
- # ... 其他配置 ...
- # 创建WebDriver对象
- edge_driver = webdriver.Edge(options=edge_options)
复制代码 对于 Google Chrome:
- from selenium import webdriver
- chrome_options = webdriver.ChromeOptions()
- chrome_options.add_argument("--incognito") # 无痕模式
- chrome_options.add_argument("--disable-extensions") # 禁用扩展
- chrome_options.add_argument("--disable-popup-blocking") # 禁用弹出拦截
- chrome_options.add_argument("test-type") # 设置测试模式
- chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"]) # 配置实验选项
- # ... 其他配置 ...
- # 创建WebDriver对象
- chrome_driver = webdriver.Chrome(options=chrome_options)
复制代码 对于 Internet Explorer:
- from selenium import webdriver
- ie_options = webdriver.IeOptions()
- ie_options.ignore_protected_mode_settings = True # 忽略IE保护模式设置
- ie_options.ignore_zoom_level = True # 忽略缩放级别
- ie_options.require_window_focus = False # 要求窗口聚焦
- ie_options.native_events = False # 是否启用原生事件
- # ... 其他配置 ...
- # 创建WebDriver对象
- ie_driver = webdriver.Ie(options=ie_options)
复制代码 请注意,代码示例中使用的参数大概会根据你的实际需求和欣赏器版本的不同而有所调解。而且在编写这些代码时,需要确保你已经安装了对应欣赏器的 WebDriver。另外,IeOptions 在最新版本的 Selenium 中大概已经被弃用,因为 Internet Explorer 欣赏器本身已经被 Microsoft 制止更新和支持了。
末了,如果您筹划实际运行这些代码,您需要确保下载了相应的 WebDriver 执行文件,并将其放置在体系的 PATH 中大概在代码中指定它的路径。
建议在编写跨欣赏器的测试脚本时,查阅官方文档以了解最新和最详细的信息,因为欣赏器和WebDriver的更新大概会引入新的选项或弃用旧的选项。同时,注意根据本身的欣赏器版本选择正确版本的驱动器。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |