python - 类型错误 : __init__() got multiple values for argument 'options'

标签 python python-3.x selenium-webdriver

引发此错误的可能原因是什么:

Traceback (most recent call last):
  File "/Users/me/sc/sc.py", line 30, in <module>
    driver = Chrome(ChromeDriverManager().install(), options=chrome_options)
TypeError: __init__() got multiple values for argument 'options'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/me/sc/sc.py", line 34, in <module>
    driver = Chrome("./chromedriver", options=chrome_options)
TypeError: __init__() got multiple values for argument 'options'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/me/sc/sc.py", line 36, in <module>
    driver = Chrome("chromedriver.exe", options=chrome_options)
TypeError: __init__() got multiple values for argument 'options'

List item

对于此代码:

from time import time, sleep

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains
from args_parser import ArgsParser
from downloader import download_file

args = ArgsParser()


def print_if_verbose(val):
    if args.output_verbose:
        print(val)


WAITING_TIMEOUT = 180
chrome_options = Options()
driver_user_agent = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                     '(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36')
chrome_options.add_argument(f'user-agent={driver_user_agent}')
if not args.display_browser:
    chrome_options.add_argument('--headless')
try:
    driver = Chrome(ChromeDriverManager().install(), options=chrome_options)
except Exception as e:
    print(e)
    try:
        driver = Chrome("./chromedriver", options=chrome_options)
    except Exception:
        driver = Chrome("chromedriver.exe", options=chrome_options)

顺便说一句,我在 macOS M2 芯片上安装了 Chrome 114,并使用 Python 3.9

最佳答案

这是由于 selenium 4.10.0 中的更改所致: https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

Changes_in_selenium_4_10_0

请注意,第一个参数不再是executable_path,而是options。 (这就是为什么它提示你两次传递它。)

如果您想传递 executable_path,您现在必须使用 service 参数。示例:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service(executable_path=r'./chromedriver')
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()

另请注意,驱动程序管理器现在内置于 selenium 中,因此您不再需要使用单独的 webdriver_manager。 Selenium 团队在这里讨论了这一点:https://www.linkedin.com/pulse/selenium-manager-best-tool-from-you-can-forget-david-burns/

关于python - 类型错误 : __init__() got multiple values for argument 'options' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76444501/

相关文章:

python - 为什么 Selenium 返回一个空文本字段?

javascript - $browser.notifyWhenNoOutstandingRequests 不考虑 Controller 功能

python - 如何在 python 中处理意外的参数类型

python - 我应该将 pyc 文件置于版本控制之下吗?

html - 表数据的正确 xpath Scrapy

python - pyparsing如何使用infixNotation来表示iif(cond, if true, if false)

java - 通过 TestNG 运行测试时出现空指针异常

python - 找不到 webdriverprefs.json - pyinstaller

python - Python 3 中的 urllib 使用

python-3.x - 使用 matplotlib 将 3D 散点图动画化为 gif 最终为空