繁体   English   中英

TypeError: __init__() 使用 Selenium 到 Python 获得了参数“executable_path”的多个值

[英]TypeError: __init__() got multiple values for argument 'executable_path' using Selenium through Python

错误是:

TypeError: __init__() got multiple values for argument 'executable_path' 

我看不出是什么原因造成的!

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

这是错误

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'

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

browser = webdriver.Chrome(options=chrome_options, executable_path='...')

或先指定路径:

browser = webdriver.Chrome(r'C:/Users/Lenovo/AppData/Roaming/Python/Python37/chromedriver.exe', options=chrome_options)

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

这是有问题的定义

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):
        ...

此错误消息...

TypeError: __init__() got multiple values for argument 'executable_path'

...暗示在调用RemoteWebDriver时多次提及参数 'executable_path'。


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

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)的定义:

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

因此,在 arguments 中,可以传递以调用用于ChromeDriver /Chrome executable_path路径的 RemoteWebDriver 是第一个参数,也是默认参数,如果没有具体指定Key

因此,在您的用例中,由于您没有提到 _key_for 第一个参数,它被认为是executable_path 然后再次在第二个参数中您再次提到了executable_path/对。 因此,您会看到错误:

TypeError: __init__() got multiple values for argument 'executable_path'

解决方案

您的有效代码块可以是以下任何一种:

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)

或者

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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM