[Python] Selenium4 초기 설정 & 크롬 드라이버 자동 설치

썬구리·2023년 2월 8일
0

Python

목록 보기
2/3

처음에는 Jsoup 라이브러리를 쓰려고 했지만 정적 WebParsing으로 한계가 있다고 하여,
동적으로 컨트롤이 가능한 Selenium을 선택했습니다.

Selenium 4 설치 및 업그레이드

  • mac 기준

설치

pip3 install selenium 
pip3 install selenium==4.4.3

업그레이드

pip3 install --upgrade pip
pip3 install --upgrade selenium

Selenium 4

기본 설정

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service, options=options)
  • Service 객체의 매개변수로 전달해 줄 수 있게 되었습니다.


Selenium 3

기본설정

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options)

크롬 드라이버 자동 설치

이전에는 크롬 버전과 일치하는 크롬 드라이버를 별도로 설치해서 PATH를 맞춰줘야하는 불편함이 있었지만, 자동으로 설치해주는 패키지가 나오면서 chromedriver 버전 관리에 더이상 신경 쓸 필요가 없어졌습니다.

1) webdriver-manager

pip3 install webdriver-manager

before

# selenium 4

service = ChromeService(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service, options=options)

after

from webdriver_manager.chrome import ChromeDriverManager

service = ChromeService(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)

pypi 공식 문서

2) chromedriver_autoinstaller

pip3 install chromedriver_autoinstaller

pypi 공식 문서

profile
맛있는 개발파이

0개의 댓글