TypeError:WebDriver.__init__() 为参数“options”获取了多个值
- 2025-04-10 09:46:00
- admin 原创
- 21
问题描述:
错误是:
TypeError: WebDriver.__init__() got multiple values for argument 'options'
`
代码如下:
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
browser = webdriver.Chrome(r'/usr/bin/chromedriver', options=chrome_options)
这是错误:
TypeError Traceback (most recent call last)
<ipython-input-5-9a7e59e392ae> in <cell line: 6>()
4 chrome_options.add_argument('--headless')
5
----> 6 browser = webdriver.Chrome(r'/usr/bin/chromedriver', options=chrome_options)
TypeError: WebDriver.__init__() got multiple values for argument 'options'
解决方案 1:
这是由于以下变化造成的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'/usr/bin/chromedriver')
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()
解决方案 2:
我从这个博客得到了这个答案并且它有效!
https://nariyoo.com/python-how-to-run-selenium-in-google-colab/
[Python] 如何在 Google Colab 中运行 selenium
!pip install chromedriver-autoinstaller
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
import time
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
import chromedriver_autoinstaller
# setup chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless') # ensure GUI is off
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# set path to chromedriver as per your configuration
chromedriver_autoinstaller.install()
# set the target URL
url = "put-url-here-to-scrape"
# set up the webdriver
driver = webdriver.Chrome(options=chrome_options)
现在您可以轻松导入所需的其他 selenium 库,驱动程序将开始工作。希望对您有所帮助!
解决方案 3:
selenium 驱动程序不起作用的解决方案
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
请检查是否在 Chromedriver 路径末尾添加了 .exe
service = Service(executable_path=r"..chromedriver.exe")
driver = webdriver.Chrome(service=service)
它对我有用,希望 selenium 版本 4.1 chrome 116 是最新版本
相关推荐
热门文章
项目管理软件有哪些?
热门标签
曾咪二维码
扫码咨询,免费领取项目管理大礼包!
云禅道AD