错误是:
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'
发布于 2020-06-02 17:53:30
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):
...
发布于 2020-06-02 18:24:48
这个错误信息..。
TypeError: __init__() got multiple values for argument 'executable_path'
...implies,即在调用RemoteWebDriver时多次提到参数'executable_path‘。
根据selenium.webdriver.chrome.webdriver.WebDriver类实现:
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
因此,在调用ChromeDriver/Chrome RemoteWebDriver的参数中,第一个参数是第一个参数,也是默认参数,在没有具体指定键的情况下。
因此,在您的usecase中,由于您还没有提到_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')
https://stackoverflow.com/questions/62158029
复制相似问题