[Python] 파이썬 selenium WebDriverException오류 해결 : selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH
selenium 테스트 코드를 실행해보았습니다. 여기저기서 셀레니움, 셀레늄 하길래…궁금했거든요. 그러나 오류를 직면했네요.
[파이썬 스크립트]
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://google.com")
# Get element with tag name 'div'
element = driver.find_element(By.TAG_NAME, 'div')
# Get all the elements available with tag name 'p'
elements = element.find_elements(By.TAG_NAME, 'p')
for e in elements:
print(e.text)
[오류 내용]
C:UsersilikeAppDataLocalProgramsPythonPython39python.exe C:/python/Workspace/main.py
Traceback (most recent call last):
File "C:UsersilikeAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdrivercommonservice.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:UsersilikeAppDataLocalProgramsPythonPython39libsubprocess.py", line 947, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:UsersilikeAppDataLocalProgramsPythonPython39libsubprocess.py", line 1416, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 지정된 파일을 찾을 수 없습니다
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:pythonWorkspacemain.py", line 4, in <module>
driver = webdriver.Chrome()
File "C:UsersilikeAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverchromewebdriver.py", line 73, in __init__
self.service.start()
File "C:UsersilikeAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdrivercommonservice.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Process finished with exit code 1
■해결방법
webdriver.Chomre()호출시 절대경로를 지정합니다.
driver = webdriver.Chrome(executable_path='C:/python/chromedriver_win32/chromedriver.exe')
아래 경로에서 PC에 설치된 크롬브라우저의 버전을 확인후 버전에 맞는 chromdriver를 다운받아요.
sites.google.com/a/chromium.org/chromedriver/downloads
리눅스용(linux64), 윈도우용(win32), 맥용(mac64) chromdriver를 다운받을 수 있습니다.
다운받은 파일(chromdriver_win32.zip)을 압축을 푼 후 본인이 원하는 경로에 복사합니다.
그리고 executable_path에 절대경로를 지정합니다.
from selenium import webdriver
from selenium.webdriver.common.by import By
#For Windows
#driver = webdriver.Chrome(r"C:UsersUSER_NAMEDesktopFOLDERchromedriver")
driver = webdriver.Chrome(executable_path="C:/python/chromedriver_win32/chromedriver.exe")
driver.get("https://google.com")
# Get element with tag name 'div'
element = driver.find_element(By.TAG_NAME, 'div')
# Get all the elements available with tag name 'p'
elements = element.find_elements(By.TAG_NAME, 'p')
for e in elements:
print(e.text)
driver.quit()
■또 다른 해결방법
명령프롬프트에서 pip install webdriver manager 명령어를 실행하여 webdriver를 설치합니다.
그리고 난 후 ChromDriverManager().install()를 사용하여 절대경로를 지정하지 않아도 해결이 가능합니다.
Microsoft Windows [Version 10.0.19042.685]
(c) 2020 Microsoft Corporation. All rights reserved.
C:Usersilike>pip install webdriver-manager
Collecting webdriver-manager
Downloading webdriver_manager-3.2.2-py2.py3-none-any.whl (16 kB)
Requirement already satisfied: requests in c:usersilikeappdatalocalprogramspythonpython39libsite-packages (from webdriver-manager) (2.25.0)
Collecting configparser
Downloading configparser-5.0.1-py3-none-any.whl (22 kB)
Collecting crayons
Downloading crayons-0.4.0-py2.py3-none-any.whl (4.6 kB)
Requirement already satisfied: colorama in c:usersilikeappdataroamingpythonpython39site-packages (from crayons->webdriver-manager) (0.4.4)
Requirement already satisfied: chardet<4,>=3.0.2 in c:usersilikeappdatalocalprogramspythonpython39libsite-packages (from requests->webdriver-manager) (3.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:usersilikeappdatalocalprogramspythonpython39libsite-packages (from requests->webdriver-manager) (1.26.2)
Requirement already satisfied: idna<3,>=2.5 in c:usersilikeappdatalocalprogramspythonpython39libsite-packages (from requests->webdriver-manager) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in c:usersilikeappdatalocalprogramspythonpython39libsite-packages (from requests->webdriver-manager) (2020.12.5)
Installing collected packages: crayons, configparser, webdriver-manager
Successfully installed configparser-5.0.1 crayons-0.4.0 webdriver-manager-3.2.2
C:Usersilike>
파이썬 스크립트(Python script)
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
[REFERENCE]
www.selenium.dev/documentation/en/webdriver/web_element/
stackoverflow.com/questions/40555930/selenium-chromedriver-executable-needs-to-be-in-path
www.edureka.co/community/3423/exceptions-webdriverexception-chromedriver-executable