JSITCLUB

selenium + 웹드라이버(ChromeDriver) 설치 본문

설치 및 설정

selenium + 웹드라이버(ChromeDriver) 설치

jsitclub 2020. 6. 20. 16:56

selenium와 웹 드라이버를설치해서 파이썬으로 브라우저를 제어해보려 한다.

1.selenimum 설치

pip install selenium

 

2. 웹 드라이버 다운로드

사용하는 브라우저에 따라 WebDriver를 다운로드하여 설치하면 된다.

Chrome https://sites.google.com/a/chromium.org/chromedriver/downloads
Edge https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox https://github.com/mozilla/geckodriver/releases
Safari https://webkit.org/blog/6900/webdriver-support-in-safari-10/

내경 우는 Chrome을 사용하고 있으므로 ChromeDriver를 다운로드할 것이다.

여기서 중요한 것은 자신의 Chrome버전에 맞는 드라이버를 다운로드해야 한다.

(크롬  버전 정보 : 크롬 브라우저의 오른쪽 맨 위의 점 세 개 버튼 > 도움말 > Chrome 정보)

다운로드한 압축파일을 풀고, 소스가 있는 폴더에 넣는다.(나중에 다른 곳에 넣고 경로를 바꾸어  주어도 된다.)

3. 파이썬

import time
from selenium import webdriver

driver = webdriver.Chrome('./chromedriver')  # 중요!! 위치를 잘맞춰준다.
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()

 

(추가 2020.06.28)

1. 위의 코드는 리눅스일 때 사용 코드이므로 window에서는 드라이버 설정을 

driver= webdriver.Chrome('chromedriver.exe')

로 바꿔준다. 

또한 chromedriver 파일을 찾을 수 없다고 나온다면, 다음과 같이 모두 써주든지 아니면 파이 썬파일과 같은 폴더에 넣어두면 실행이 된다( 단, 내 경우에는 이때도 python IDLE는 바로 됐지만 vscode를 사용할 때는 밑의 터미널의 경로의 영향을 받았다. 어쨌든 chromedriver 파일의 위치가 무척 중요하다.)

 

Comments