init() got an unexpected keyword argument ‘desired_capabilities’

Posted in :

在使用 selenium 遇到這個問題, 原因是在 selenium 4.10 版後, desired_capabilities 被刪除了造成, 解法非常簡單, 因為原本要透過 desired_capabilities 設定的參數, 變成直接透過 options 就可以存取了.

解法:

This is due to changes in selenium 4.10.0https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

Changes_in_selenium_4_10_0

Note that desired_capabilities has been removed from the __init__, but there is now another way of passing it in. See https://www.selenium.dev/documentation/webdriver/getting_started/upgrade_to_selenium_4/#capabilities for the documentation on how to pass in desired capabilities when using selenium 4.10.0 (or newer).

Here’s a code snippet on using capabilities in the new version:

from selenium.webdriver.firefox.options import Options as FirefoxOptions
options = FirefoxOptions()
cloud_options = {}
cloud_options['build'] = "build_1"
cloud_options['name'] = "test_abc"
options.set_capability('cloud:options', cloud_options)
driver = webdriver.Remote("http://0.0.0.0:4723/wd/hub", options=options)

常用的幾個參數, 新的寫法:

chrome_options.page_load_strategy = 'eager'
chrome_options.unhandled_prompt_behavior = "accept"

官方的 source code:
https://github.com/SeleniumHQ/selenium/blob/ca60c2207f15c014fa47448860a19db67c020df3/py/selenium/webdriver/common/options.py#L99

@property
def capabilities(self):
    return self._caps

def set_capability(self, name, value) -> None:
    """Sets a capability."""
    self._caps[name] = value

@property
def browser_version(self) -> str:
    """
    :returns: the version of the browser if set, otherwise None.
    """
    return self._caps.get("browserVersion")

@browser_version.setter
def browser_version(self, version: str) -> None:
    """Requires the major version of the browser to match provided value:
    https://w3c.github.io/webdriver/#dfn-browser-version.

    :param version: The required version of the browser
    """
    self.set_capability("browserVersion", version)

@property
def platform_name(self) -> str:
    """
    :returns: The name of the platform
    """
    return self._caps["platformName"]

@platform_name.setter
def platform_name(self, platform: str) -> None:
    """Requires the platform to match the provided value:
    https://w3c.github.io/webdriver/#dfn-platform-name.

    :param platform: the required name of the platform
    """
    self.set_capability("platformName", platform)

@property
def page_load_strategy(self) -> str:
    """
    :returns: page load strategy if set, the default is "normal"
    """
    return self._caps["pageLoadStrategy"]

@page_load_strategy.setter
def page_load_strategy(self, strategy: str) -> None:
    """Determines the point at which a navigation command is returned:
    https://w3c.github.io/webdriver/#dfn-table-of-page-load-strategies.

    :param strategy: the strategy corresponding to a document readiness state
    """
    if strategy in ["normal", "eager", "none"]:
        self.set_capability("pageLoadStrategy", strategy)
    else:
        raise ValueError("Strategy can only be one of the following: normal, eager, none")

@property
def unhandled_prompt_behavior(self) -> str:
    """
    :returns: unhandled prompt behavior if set, the default is "dismiss and notify"
    """
    return self._caps["unhandledPromptBehavior"]

@unhandled_prompt_behavior.setter
def unhandled_prompt_behavior(self, behavior: str) -> None:
    """How the driver should respond when an alert is present and the
    command sent is not handling the alert:
    https://w3c.github.io/webdriver/#dfn-table-of-page-load-strategies.

    :param behavior: behavior to use when an alert is encountered
    """
    if behavior in ["dismiss", "accept", "dismiss and notify", "accept and notify", "ignore"]:
        self.set_capability("unhandledPromptBehavior", behavior)
    else:
        raise ValueError(
            "Behavior can only be one of the following: dismiss, accept, dismiss and notify, "
            "accept and notify, ignore"
        )

@property
def timeouts(self) -> dict:
    """
    :returns: Values for implicit timeout, pageLoad timeout and script timeout if set (in milliseconds)
    """
    return self._caps["timeouts"]

@timeouts.setter
def timeouts(self, timeouts: dict) -> None:
    """How long the driver should wait for actions to complete before
    returning an error https://w3c.github.io/webdriver/#timeouts.

    :param timeouts: values in milliseconds for implicit wait, page load and script timeout
    """
    if all(x in ("implicit", "pageLoad", "script") for x in timeouts.keys()):
        self.set_capability("timeouts", timeouts)
    else:
        raise ValueError("Timeout keys can only be one of the following: implicit, pageLoad, script")

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *