ubuntu 中 chromedriver遇到以下错误TypeError: urlopen() got multiple values for keyword argument 'body'

本文介绍了一种常见的Selenium结合ChromeDriver在自动化测试中遇到的问题及解决方案。通过卸载并重新安装urllib3库,成功解决了程序运行时出现的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出错程序如下:

    # -*- coding: utf-8 -*-
    from bs4 import BeautifulSoup
    import time
    from selenium import webdriver
    import os
    chromedriver = "./chromedriver"
    os.environ["webdriver.chrome.driver"] = chromedriver
    driver = webdriver.Chrome(chromedriver)
    
    url="https://www.baidu.com/"
    driver.get(url)

解决方法如下:

(1) 先卸载已有的urllib3:

 sudo apt-get remove python-urllib3

(2)重新安装urllib3:

sudo pip install -U urllib3

不知道能不能解决所有人的问题,但是解决我的了QAQ

<think>好的,我现在要解决用户提出的关于Selenium在Python中初始化WebDriver时出现的TypeError问题,具体是“WebDriver.__init__() got multiple values for argument 'options'”。首先,我需要理解这个错误的原因,然后根据用户提供的引用信息找到解决方案。 首先,用户提到了引用[2]和引用[3],其中提到Selenium最新版本(比如4.10)的语法变化。特别是executable_path参数被移除了,需要使用Service对象来替代。同时,当使用options参数时,如果同时传递了位置参数和关键字参数,可能会导致重复赋值,从而引发多个值错误。 接下来,我需要回忆Selenium的WebDriver初始化方式的变化。在旧版本中,常用的方式可能是这样的: ```python from selenium import webdriver driver = webdriver.Chrome(executable_path='chromedriver.exe', options=options) ``` 但在新版本中,executable_path参数被废弃,必须通过Service类来设置驱动路径。同时,如果同时传递了位置参数和关键字参数,例如在Service初始化时可能传入了options,或者在WebDriver的初始化中错误地混用了新旧参数,就会导致冲突。 根据引用[2]中的示例代码,正确的做法是使用Service对象,并将options作为参数传递给webdriver.Chrome的options参数,而不是Service。例如: ```python from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager options = webdriver.ChromeOptions() options.add_argument("--headless") service = ChromeService(executable_path=ChromeDriverManager().install()) driver = webdriver.Chrome(service=service, options=options) ``` 但这里可能存在一个问题,如果Service的初始化中包含了某些参数,或者WebDriver的__init__方法在接收参数时,位置参数和关键字参数冲突了,就会导致“multiple values for argument 'options'”错误。 可能用户在使用时,错误地将options同时传递给了Service和WebDriver,或者在新版本中某些参数的传递方式发生了变化。例如,在旧代码中可能同时传递了options作为位置参数和关键字参数,导致重复。 我需要检查用户提供的代码示例。引用[2]中的正确代码是: ```python driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install())) ``` 但用户可能在自己的代码中混用了旧参数和新参数,例如: ```python driver = webdriver.Chrome(ChromeDriverManager().install(), options=options) ``` 在这种情况下,第一个参数(位置参数)会被视为executable_path,而新版本中executable_path已经被移除,所以可能变成了其他参数,如service。或者,如果用户同时传递了service和options,但框架内部处理时参数的位置导致重复。 例如,假设WebDriver的__init__方法定义如下: def __init__(self, service, options): 如果用户同时传递了service的位置参数,然后又传递了options作为关键字参数,可能会导致冲突。或者,用户可能错误地将options传递到了Service的初始化中,而同时又传递给了WebDriver的options参数,导致重复。 另一个可能性是,用户在使用WebDriver-manager时,同时传递了service和executable_path参数,或者新旧参数混用导致冲突。 因此,解决方案应该是确保正确使用Service对象,并将options单独传递给WebDriver的options参数,而不与Service的参数混淆。此外,确保没有重复传递参数,例如既传递service对象,又传递executable_path作为位置参数,这可能引起冲突。 总结可能的解决步骤: 1. 检查Selenium版本是否升级到最新(4.10及以上),并确认语法变化。 2. 使用Service类来设置驱动路径,而不是直接传递executable_path。 3. 确保options参数仅传递给WebDriver的options关键字参数,而不是Service。 4. 检查代码中是否同时传递了位置参数和关键字参数导致重复。 5. 参考官方文档或引用中的示例代码进行调整。 现在,根据这些分析,构建一个结构清晰的回答,帮助用户逐步解决问题,并引用相关参考资料。</think>针对Selenium中因参数传递冲突导致的`TypeError: WebDriver.__init__() got multiple values for argument 'options'`问题,以下是分步解决方案: --- ### **问题原因** 1. **Selenium版本升级**:从4.10版本开始,`executable_path`参数被废弃,需改用`Service`类管理浏览器驱动路径[^3]。 2. **参数传递冲突**:新旧语法混用或同时通过位置参数和关键字参数传递`options`,导致参数重复赋值[^2]。 --- ### **解决方案** #### **步骤1:检查并更新依赖库** 确保使用最新版本的Selenium和WebDriver Manager: ```bash pip install --upgrade selenium webdriver-manager ``` #### **步骤2:调整代码语法** 使用`Service`类替代`executable_path`,并将`options`单独传递给WebDriver: ```python from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager # 配置浏览器选项 options = webdriver.ChromeOptions() options.add_argument("--headless") # 示例:无头模式 # 初始化Service对象 service = ChromeService(executable_path=ChromeDriverManager().install()) # 正确传递参数:service和options分开 driver = webdriver.Chrome(service=service, options=options) ``` #### **步骤3:避免参数冲突** - **错误示例**:混用新旧参数导致重复 ```python driver = webdriver.Chrome(ChromeDriverManager().install(), options=options) # 触发TypeError ``` 此处第一个参数被识别为`service`,但未显式声明参数类型,可能引发冲突。 - **正确做法**:显式使用`service=`和`options=`关键字参数。 #### **步骤4:验证其他场景** 若需自定义驱动路径,直接通过`Service`设置: ```python service = ChromeService(executable_path="/path/to/chromedriver") driver = webdriver.Chrome(service=service, options=options) ``` --- ### **注意事项** - **参数顺序和命名**:新版Selenium严格要求参数命名,避免省略关键字(如`service=`或`options=`)。 - **官方文档参考**:升级语法时参考[Selenium 4迁移指南](https://github.com/SeleniumHQ/seleniumhq.github.io/blob/trunk/website_and_docs/content/documentation/webdriver/getting_started/upgrade_to_selenium_4.en.md)[^3]。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值