Chrome TypeError:__init__()通过Python使用Selenium为参数'executable_path'获取了多个值

lfapxunr  于 2023-06-19  发布在  Go
关注(0)|答案(2)|浏览(277)

错误是:

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

f1tvaqid1#

executable_path是由Chrome定义的第一个位置参数,因此这就是第一个位置参数chrome_options所绑定的。然后尝试设置相同的参数 * 再次 *,这次使用关键字参数。使用关键字参数指定两者:

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

kknvjkwl2#

此错误消息...

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

因此,在可以传递的参数中调用 RemoteWebDriver for ChromeDriver/Chrome executable_path的参数是第一个参数,也是默认参数,以防 Key 没有特别指定。
因此,在您的用例中,由于您没有提到第一个参数的_key_,它被认为是executable_pathvalue。在第二个参数中,你再次提到了executable_pathKey / Value 对。因此,您可以看到错误:

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

相关问题