首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError:__init__()通过__init__获得了使用Selenium参数'executable_path‘的多个值

TypeError:__init__()通过__init__获得了使用Selenium参数'executable_path‘的多个值
EN

Stack Overflow用户
提问于 2020-06-02 17:39:14
回答 2查看 1.7K关注 0票数 1

错误是:

代码语言:javascript
运行
复制
TypeError: __init__() got multiple values for argument 'executable_path' 

我不知道是什么导致了这一切!

代码语言:javascript
运行
复制
chrome_options = Options() #line 8
chrome_options.add_argument('--headless') #line 9
chrome_options.add_argument('--no-sandbox') #line 10
chrome_options.add_argument('--disable-gpu') #line 11
browser=webdriver.Chrome(chrome_options,executable_path=r'C:/Users/Lenovo/AppData/Roaming/Python/Python37/chromedriver.exe') #This is line 12

这是错误

代码语言:javascript
运行
复制
Traceback (most recent call last):
File "main.py", line 12, in <module>
browser = webdriver.Chrome(chrome_options, executable_path=r'C:/Users/Lenovo/AppData/Roaming/Python/Python37/chromedriver.exe')
TypeError: __init__() got multiple values for argument 'executable_path'
EN

回答 2

Stack Overflow用户

发布于 2020-06-02 17:53:30

executable_pathChrome定义的第一个位置参数,所以第一个位置参数chrome_options是绑定到的。然后再次尝试设置相同的参数,这一次使用关键字参数。使用关键字参数指定这两种方法:

代码语言:javascript
运行
复制
browser = webdriver.Chrome(options=chrome_options, executable_path='...')

或首先指定路径:

代码语言:javascript
运行
复制
browser = webdriver.Chrome(r'C:/Users/Lenovo/AppData/Roaming/Python/Python37/chromedriver.exe', options=chrome_options)

我仍然对选项使用关键字参数,因为options是第三个位置参数;port是第二个位置参数。

这是所涉定义

代码语言:javascript
运行
复制
class WebDriver(ChromiumDriver):
    """
    Controls the ChromeDriver and allows you to drive the browser.
    You will need to download the ChromeDriver executable from
    http://chromedriver.storage.googleapis.com/index.html
    """

    def __init__(self, executable_path="chromedriver", port=DEFAULT_PORT,
                 options=None, service_args=None,
                 desired_capabilities=None, service_log_path=DEFAULT_SERVICE_LOG_PATH,
                 chrome_options=None, service=None, keep_alive=True):
        ...
票数 0
EN

Stack Overflow用户

发布于 2020-06-02 18:24:48

这个错误信息..。

代码语言:javascript
运行
复制
TypeError: __init__() got multiple values for argument 'executable_path'

...implies,即在调用RemoteWebDriver时多次提到参数'executable_path‘。

根据selenium.webdriver.chrome.webdriver.WebDriver类实现:

代码语言:javascript
运行
复制
class selenium.webdriver.chrome.webdriver.WebDriver(executable_path='chromedriver', port=0, options=None, service_args=None, desired_capabilities=None, service_log_path=None, chrome_options=None)

此外,根据Chrome的WebDriver(RemoteWebDriver)定义:

代码语言:javascript
运行
复制
def __init__(self, executable_path="chromedriver", port=0,
             options=None, service_args=None,
             desired_capabilities=None, service_log_path=None,
             chrome_options=None):
    """
    Creates a new instance of the chrome driver.

    Starts the service and then creates new instance of chrome driver.

    :Args:
     - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH

因此,在调用ChromeDriver/Chrome RemoteWebDriver的参数中,第一个参数是第一个参数,也是默认参数,在没有具体指定键的情况下。

因此,在您的usecase中,由于您还没有提到_key_for的第一个参数,所以它被认为是executable_path的值。在第二个参数中,再次提到了executable_path的键/值对。因此,您可以看到错误:

代码语言:javascript
运行
复制
TypeError: __init__() got multiple values for argument 'executable_path'

解决方案

有效的代码块可以是以下任一项:

代码语言:javascript
运行
复制
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
browser=webdriver.Chrome(executable_path=r'C:/Users/Lenovo/AppData/Roaming/Python/Python37/chromedriver.exe', options=chrome_options)

代码语言:javascript
运行
复制
chrome_options = Options() #line 8
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
browser=webdriver.Chrome(options=chrome_options, executable_path=r'C:/Users/Lenovo/AppData/Roaming/Python/Python37/chromedriver.exe')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62158029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档