goodthings4me.tistory.com
파이썬 크롬드라이버 자동설치를 위한 코드. selenium 사용 시 크롬 웹브라우저에 맞는 크롬드라이버를 자동으로 다운로드하여 사용할 때 유용하다.
◆ 파이썬 크롬드라이버 자동 설치해보기 (윈도우 환경 기준)
컴퓨터에 설치된 크롬브라우저 버전 확인은 브라우저 주소에 "chrome://settings/help"라고 입력해도 되고, 아래처럼 도움말에서 Chrome 정보를 클리해서 확인해도 된다.
그리고, 크롬 드라이버 다운로드 웹 사이트는 https://chromedriver.chromium.org/downloads
ChromeDriver - WebDriver for Chrome - Downloads
Current Releases If you are using Chrome version 109, please download ChromeDriver 109.0.5414.25 If you are using Chrome version 108, please download ChromeDriver 108.0.5359.71 If you are using Chrome version 107, please download ChromeDriver 107.0.5304.62
chromedriver.chromium.org
위 사이트에 접속하면 아래처럼 크롬 드라이버 버전별로 chromedriver.exe 파일을 다운로드할 수 있다.
하지만, 파이썬에서 selenium 사용 시 크롬 브라우저 버전이 업데이트되면 다시 크롬 드라이버를 다운로드해야하기 때문에 번거로운 경우가 많다. 이럴 때 크롬 브라우저에 맞는 크롬 드라이버를 자동으로 다운로드하는 코드를 작성해 사용하면 편리한다.
※ selenium 라이브러리 설치
pip install selenium
※ 크롬 드라이버 자동 다운로드 라이브러리 설치
pip install chromedriver-autoinstaller
▶ 크롬 드라이버 자동 다운로드 코드
import os
from selenium import webdriver
import chromedriver_autoinstaller
# 크롬 브라우저 버전 확인하기
chrome_ver = chromedriver_autoinstaller.get_chrome_version()
print(chrome_ver) # 108.0.5359.125
chromedriver_autoinstaller.install(True)
chromedriver_path = f'./{chrome_ver.split(".")[0]}/chromedriver.exe'
print(chromedriver_path) # ./108/chromedriver.exe
print(os.path.exists(chromedriver_path)) # True
print(os.path.basename(chromedriver_path)) # chromedriver.exe
url = 'https://www.coupang.com'
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(3)
- 크롬 드라이버 자동 설치 import는 import chromedriver-autoinstaller 아니라 import chromedriver_autoinstaller 이며,
- chromedriver_autoinstaller.install(True) 처럼 'True'가 있어야 폴더가 생성되고 파일이 다운로드된다.
- 설치 폴더는 크롬브라우저 버전 108.0.5359.125에서 앞 108로 생성되기 때문에 split('.')[0] 처리하여 확인했으며,
- 설치 폴더와 chromedriver.exe 파일이 있는지 없는지 if 조건문으로 처리해도 되나 파일이 있더라도 업데이트 되도록 했다.
'코딩 연습' 카테고리의 다른 글
네이버 영화 평점 리뷰 추출해보기 (with 파이썬) (0) | 2022.12.30 |
---|---|
이미지 병합 - 파이썬 Tkinter 활용 (0) | 2022.12.28 |
문자열에 \xa0 있을 경우 제거하는 방법 (0) | 2022.12.02 |
공동주택 단지코드(아파트 코드) 추출 - 파이썬 API 활용 (0) | 2022.11.30 |
엑셀 암호 해제 방법 - 파이썬으로 엑셀 암호 제거 프로그램 만들기 (0) | 2022.10.13 |
댓글