selenium 사용 시, 크롬 브라우저와 같은 버전 드라이버를 설치해도 코드 실행 시 바로 꺼지는 현상
원본 코드
# 1) pipenv install selenium
# 2) https://chromedriver.chromium.org/downloads
# 크롬 버전과 동일한 드라이버 버전을 다운받아, 원하는 폴더에서 압축해제
from selenium import webdriver
browser = webdriver.Chrome("C:/Users/everp/Documents/env_p3/chromedriver.exe")
browser.get("https://www.naver.com")
개선 코드
# 1) pipenv install selenium
# 2) https://chromedriver.chromium.org/downloads
# 크롬 버전과 동일한 드라이버 버전을 다운받아, 원하는 폴더에서 압축해제
# 3) pipenv install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# 크롬 드라이버 자동 업데이트
from webdriver_manager.chrome import ChromeDriverManager
# 브라우저 꺼짐 방지
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
# 불필요한 에러 메시지 없애기
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"])
service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options,executable_path="C:/Users/everp/Documents/env_p3/chromedriver.exe")
# 웹페이지 해당 주소 이동
driver.get("https://www.naver.com")
늘 변화하고 개선되는 코드들. 안주하면 끝이다. 그래서 매력적이고 공정한 생활코딩의 세계 ! ㅎㅎㅎ
가장 중요한 공식문서
https://www.selenium.dev/selenium/docs/api/py/index.html
Selenium Client Driver — Selenium 4.8 documentation
www.selenium.dev
