TypeError: WebDriver.init() got an unexpected keyword argument 'executable_path'
я не могу понять в чем ошибка?
Traceback (most recent call last):
File
"/home/zer0/PycharmProjects/PythonProject/pet_project_phone/pet_projec
t_phone.py", line 3, in <module>
driver = webdriver.Chrome(executable_path =
'/home/zer0/PycharmProjects/PythonProject/pet_project_phone/chromedriv
er-linux64/chromedriver')
TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path'
from selenium import webdriver
driver = webdriver.Chrome(executable_path =
'/home/zer0/PycharmProjects/PythonProject/pet_project_phone/chromedriv
er-linux64/chromedriver')
Ответы (1 шт):
Автор решения: SVBazuev
Если посмотришь на исходный код класса
→ Ссылка
Если посмотришь на исходный код класса WebDriver:
class WebDriver(ChromiumDriver):
"""Controls the ChromeDriver and allows you to drive the browser."""
def __init__(
self,
options: Optional[Options] = None,
service: Optional[Service] = None,
keep_alive: bool = True,
) -> None:
"""Creates a new instance of the chrome driver. Starts the service and
then creates new instance of chrome driver.
:Args:
- options - this takes an instance of ChromeOptions
- service - Service object for handling the browser driver if you need to pass extra details
- keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
"""
service = service if service else Service()
options = options if options else Options()
super().__init__(
browser_name=DesiredCapabilities.CHROME["browserName"],
vendor_prefix="goog",
options=options,
service=service,
keep_alive=keep_alive,
)
То причина этой ошибки:
TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path'
сразу станет очевидной.
Дело в том, что с версии 4.0 изменился способ инициализации драйвера.
И когда Вам требуется явно указать путь к конкретной версии драйвера,
делать это надо через класс Service, взгляни на его код:
class Service(service.ChromiumService):
"""A Service class that is responsible for the starting and stopping of
`chromedriver`.
:param executable_path: install path of the chromedriver executable, defaults to `chromedriver`.
:param port: Port for the service to run on, defaults to 0 where the operating system will decide.
:param service_args: (Optional) List of args to be passed to the subprocess when launching the executable.
:param log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file.
:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
"""
def __init__(
self,
executable_path: Optional[str] = None,
port: int = 0,
service_args: Optional[List[str]] = None,
log_output: Optional[SubprocessStdAlias] = None,
env: Optional[Mapping[str, str]] = None,
**kwargs,
) -> None:
super().__init__(
executable_path=executable_path,
port=port,
service_args=service_args,
log_output=log_output,
env=env,
**kwargs,
)
^^^ и вот тут уже есть именнованный аргумент executable_path
В результате код будет выглядеть так:
from selenium import webdriver
service = webdriver.ChromeService(executable_path='C:\\ProgramData\\chocolatey\\bin\\chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get("https://www.yandex.ru")
driver.implicitly_wait(5)
driver.quit()