引发此错误的可能原因是什么:
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
请注意,第一个参数不再是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/