TypeError:WebDriver.__init__() 为参数“options”获取了多个值

2025-04-10 09:46:00
admin
原创
22
摘要:问题描述:错误是:TypeError: WebDriver.__init__() got multiple values for argument 'options' `代码如下:chrome_options = Options() chrome_options.add_argument('--headles...

问题描述:

错误是:

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

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'/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 是最新版本

相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   2482  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1533  
  PLM(产品生命周期管理)项目对于企业优化产品研发流程、提升产品质量以及增强市场竞争力具有至关重要的意义。然而,在项目推进过程中,范围蔓延是一个常见且棘手的问题,它可能导致项目进度延迟、成本超支以及质量下降等一系列不良后果。因此,有效避免PLM项目范围蔓延成为项目成功的关键因素之一。以下将详细阐述三大管控策略,助力企业...
plm系统   0  
  PLM(产品生命周期管理)项目管理在企业产品研发与管理过程中扮演着至关重要的角色。随着市场竞争的加剧和产品复杂度的提升,PLM项目面临着诸多风险。准确量化风险优先级并采取有效措施应对,是确保项目成功的关键。五维评估矩阵作为一种有效的风险评估工具,能帮助项目管理者全面、系统地评估风险,为决策提供有力支持。五维评估矩阵概述...
免费plm软件   0  
  引言PLM(产品生命周期管理)开发流程对于企业产品的全生命周期管控至关重要。它涵盖了从产品概念设计到退役的各个阶段,直接影响着产品质量、开发周期以及企业的市场竞争力。在当今快速发展的科技环境下,客户对产品质量的要求日益提高,市场竞争也愈发激烈,这就使得优化PLM开发流程成为企业的必然选择。缺陷管理工具和六西格玛方法作为...
plm产品全生命周期管理   0  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用