2

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)

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?

Thank You

zeroth
  • 37
  • 2
  • 7

1 Answers1

10

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.

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.

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.

JimEvans
  • 27,201
  • 7
  • 83
  • 108