Back-End/Python
[python-웹크롤링] 셀레니움(Selenium) 설치 / 크롬 드라이버(chromedriver) 설치
uni2237
2022. 9. 21. 11:33
728x90
728x90
📌 셀레니움 (Selenium) 설치
# pip
pip install selenium
# conda 환경
conda install selenium
# jupyter-notebook pip
!pip install selenium
위 3가지 중 하나 선택해서 설치하면 됨.
📌 크롬 드라이버 ( chromedriver ) 설치
Selenium을 사용해서 웹 자동화를 하려면 웹 드라이버(webdriver)를 설치해야 한다.
자신의 브라우저에 맞게 다운하면 됨. (이 글에서는 Chrome의 경우를 다룸)
👉 1. Chrome 정보 확인
우선 자신의 Chrome 버전을 확인해야 한다.

크롬 실행 -> 오른쪽 위 점 3개 클릭 -> 도움말 -> Chrome 정보 클릭

위와 같이 자신의 Chrome 버전을 확인 가능.
나는 105.XX 버전임을 확인할 수 있다.
👉 2. 버전에 맞는 크롬 드라이버 다운로드
'ChromeDriver - WebDriver for Chrome' 에서 Chrome Driver를 다운받는다
링크 : https://chromedriver.chromium.org/downloads

나의 크롬 버전은 105.xx 였으므로, 두번째 줄의 ChromeDriver 105.0.5195.52 를 클릭

자신의 os에 맞게 설치하기 !
👉 3. 다운받은 크롬 드라이버 위치 변경
우리는 크롬 드라이버를 다음과 같이 사용한다.
from selenium import webdriver
driver = webdriver.Chrome('chromedriver 경로')
원하는 프로젝트마다 크롬 드라이버 경로를 쉽게 불러올 수 있도록 위치를 변경해주자.
보통은 C:\Users\유저명 밑에 저장함.
나의 경우는 C:\Users\choyunhui\chromedriver.exe 로 저장해둠.
from selenium import webdriver
driver = webdriver.Chrome('C:/Users/choyunhui/chromedriver')
🤷♀️ 나는 이번 프로젝트에서만 쓸건데? 하시는 분들은
# 현재 경로 확인 방법
import os
os.getcwd()
사용할 해당 프로젝트로 가서 현재 경로 확인 후 , 그 안에 크롬 드라이버 옮겨주면 됨.

👉 4. Selenium webdriver 사용
from selenium import webdriver
# selenium으로 무엇인가를 입력하기 위함
from selenium.webdriver.common.keys import Keys
# 크롬 드라이버 실행 ex) C:/Users/choyunhui/chromedriver
driver = webdriver.Chrome('자신의 chromedriver 경로')
# 크롬드라이버에 원하는 url 주소 넣으면 , 해당 url을 브라우저에서 띄운다
driver.get("https://www.naver.com/")
# 브라우저 닫기
driver.close()
driver.get("https://www.naver.com/") 코드 때문에 naver가 새 창으로 뜸
끝 !
728x90
728x90