为什么在 Selenium 中使用 add_experimental_option? [英] Why is add_experimental_option used in Selenium?

查看:54
本文介绍了为什么在 Selenium 中使用 add_experimental_option?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解与以下内容非常相似的代码片段:

I am trying to understand a code snippet which is quite similar to the following :

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
port_number = "127.0.0.1.8888"
chrome_options.add_experimental_option("someAddressName", port_number)

我试图寻找解释,但直到现在还没有得到任何满意的答案.有人可以特别解释一下 Options()add_experimental_option 的任务吗?

I tried looking for the explanation but haven't got any satisfactory answer until now. Can someone please explain the task of Options() and add_experimental_option in particular?

谢谢

推荐答案

WebDriver 实现接受它们尝试自动化的浏览器的配置信息的方式是通过称为功能"的概念.启动会话(启动浏览器)时,用户指定他们希望启动的驱动程序支持哪些功能.这可能包括接受自签名 SSL 证书的能力、浏览器的配置文件设置或任何数量的其他内容.

The way WebDriver implementations accept configuration information for the browsers they try to automate is via a concept called "capabilities." When starting a session (launching a browser), the user specifies what capabilities they want the launched driver to support. This might include things like the ability to accept self-signed SSL certificates, profile settings for the browser, or any number of other things.

然而,这个模型的挑战之一是它是作为字典实现的,键值对由任意字符串的键和任意类型对象的值组成.这里有两个潜在的问题.首先是能力名称很容易忘记或打错,并且可能不是所有浏览器都支持.此外,在实际执行代码之前不可能知道这一点.至于值,通常,驱动程序期望与其支持的名称关联的值是特定类型的,同样,这在驱动程序实现之间可能会有很大差异.

One of the challenges with this model, however, is that it is implemented as a dictionary, with key-value pairs consisting of keys that are arbitrary strings and values that are objects of arbitrary type. There are two potential problems here. The first is that the capability names are easy to forget or mistype, and may not be supported by all browsers. Moreover, it's impossible to know this before the code is actually executed. As for values, usually, a driver expects that the value associated with a name that it supports be of a specific type, and again, this can differ widely between driver implementations.

输入选项类.这些构造为特定驱动程序可能期望的各种功能提供设置器.它可以强制通过功能字典传递给驱动程序的名称是正确的,并且可以对值强制类型安全.但是,大多数驱动程序实现(Chromium 项目的 chromedriver、Mozilla 的 geckodriver、Microsoft 用于 Edge 浏览器的 MicrosoftWebDriver 等)不是由或由 Selenium 项目维护.可能的情况是,驱动程序提供者发布了一个新的驱动程序版本,该版本理解 Selenium 项目没有时间在该驱动程序的选项类中创建类型安全设置器的新功能.

Enter the Options classes. These constructs provide setters for the various capabilities that a particular driver might expect. It can enforce that the name passed to the driver via the capabilities dictionary is correct, and it can enforce type-safety on the values. However, most driver implementations (the Chromium project's chromedriver, Mozilla's geckodriver, Microsoft's MicrosoftWebDriver for the Edge browser, etc.) are not built by or maintained by the Selenium project. It may be the case that a driver provider releases a new driver version that understands a new capability that the Selenium project hasn't had time to create a type-safe setter in that driver's options class.

因此,在下一个 Selenium 版本发布之前,用户不会被阻止使用新功能,选项类具有添加一个尚无类型安全设置器的附加选项"方法.在 Python 中,您的示例看起来就是用 Python 编写的,该方法称为 add_experimental_option.该方法通常打算暂时使用,直到 Selenium 项目可以推送一个新版本,该版本包含一个类型安全设置器,用于驱动程序实现者(在您的示例中为 Chrome 驱动程序)添加的任何新选项.

So that users aren't blocked from using the new capability until the next version of Selenium is released, the options classes have an "add an additional option for which there is not yet a type-safe setter" method. In Python, which is what your sample looks to be written in, that method is called add_experimental_option. That method is generally intended to be used temporarily until the Selenium project can push a new release that includes a type-safe setter for whatever new option the driver implementor (Chrome driver, in your example) has added.

这篇关于为什么在 Selenium 中使用 add_experimental_option?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆