hoim
티스토리 뷰
1. selenium 설치
$pip3 install selenium
참고: https://www.selenium.dev/documentation/
The Selenium Browser Automation Project
Selenium automates browsers. That's it!
www.selenium.dev
2. chrome driver 설치 → webdriver 설치
2-1. chrome driver 수동 설치
현재 사용하고 있는 크롬과 같은 버전의 드라이버를 설치한다.
https://chromedriver.chromium.org/downloads
ChromeDriver - WebDriver for Chrome - Downloads
Current Releases If you are using Chrome version 101, please download ChromeDriver 101.0.4951.15 If you are using Chrome version 100, please download ChromeDriver 100.0.4896.60 If you are using Chrome version 99, please download ChromeDriver 99.0.4844.51 F
chromedriver.chromium.org
* ERROR
chromedriver를 python과 동일 경로에 넣어주고 webdriver.Chrome() 으로 상대경로로 잡아주니 아래처럼 경로를 못찼는 에러가 발생했다. 절대 경로로 잡아주니 해결되었다.
해결 전,
import selenium
from selenium import webdriver
driver = webdriver.Chrome();
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see [<https://chromedriver.chromium.org/home>](<https://chromedriver.chromium.org/home>)
Traceback (most recent call last):
File "[webcralwer.py](<http://webcralwer.py/>)", line 4, in
driver = webdriver.Chrome()
File "/Users/***/Project/pythonWorkspace/env/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 70, in **init**
super(WebDriver, self).**init**(DesiredCapabilities.CHROME['browserName'], "goog",
File "/Users/***/Project/pythonWorkspace/env/lib/python3.8/site-packages/selenium/webdriver/chromium/webdriver.py", line 90, in **init**
self.service.start()
File "/Users/***/Project/pythonWorkspace/env/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see [<https://chromedriver.chromium.org/home>](<https://chromedriver.chromium.org/home>)
해결 후,
import os
import selenium
from selenium import webdriver
path = os.getcwd() + '/chromedriver';
driver = webdriver.Chrome(path);
* WARNING
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
executable_path는 이제 더이상 쓰지 않으니 Service() 인스턴스를 사용하라고 한다.
아래처럼 소스를 수정해주니 이상 없이 잘 동작한다.
from webdriver_manager.chrome import ChromeDriverManager
path = os.getcwd() + '/chromedriver'
s = Service(path)
driver = webdriver.Chrome(service=s)
2-2. webdriver 설치
그러다... 수동 설치가 아닌 자동으로 드라이브 버전을 관리하도록 소스를 수정해 주었다. 그래 수동은 좋지 않아!
webdriver-manager를 통해 사용하고 있는 브러우저 버전에 맞는 드라이버를 사용할 수 있다. 변경될 경로, 버전 걱정하지 않도록 webdriver-manager로 수정하자.
$pip3 install webdriver-manager
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()));
'PYTHON' 카테고리의 다른 글
python 웹 크롤링(2) : 구글 자동 로그인하기1 (0) | 2022.04.05 |
---|---|
python 웹 크롤링 : 뉴스레터 모아보기(진행중) (0) | 2022.02.05 |