pythom 3.8
pycharm
selrmium 4
사이트 이미지 크롤링이 필요해서 selenium을 처음 쓰기 위해 코드를 이것저것 찾아보는데 여러 소스들을 적용했을 때 일어나는 오류가 있었다!
이 오류 말고도 다른 오류가 있었지만 두 오류는 특히나 반복적으로 나타나서 여러 해결방법을 적용해보았다.
그 중 실질적으로 해결에 도움이 된 건 다음 방법이다.
나는 크게
DeprecationWarning: executable_path has been deprecated, please pass in a Service object 오류와
DeprecationWarning: use options instead of chrome_options error using ChromeDriver 오류를 경험했는데
여러 사이트를 찾아도 해결하는 방법이 쉽지 않았다.
폭풍 검색 결과 실질적으로 해결된 각 사이트는 다음과 같다.
이 문제는 크롬 버젼과 현재 쓰려는 selenium 버전이 맞지 않아서 발생하는 문제이다.
기존에 다운 받은 사이트에서 가장 최신 버전을 받았는데 알고보니 그 정보가 작성된 지 시간이 지나서 버전이 맞지 않았다.
https://chromedriver.chromium.org/downloads
이 사이트에가서 최신 버전을 받으면 된다.
가장 최신인 selenium 4 버전을 이용하게 되면서 코드 작성 방식에 수정 사항이 있었다. 이걸 모르고 3버전의 코드를 작성하였더니 오류가 생겼던 것이다.
#기존 버전 3 코드
driver = webdriver.Chrome('C:\chromedriver\chromedriver.exe')
위에 있는 코드를 아래처럼 수정해야한다.
#기존 버전 4 코드
s = Service('C:\chromedriver_win32\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=chrome_options)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(("lang=ko_KR"))
driver = webdriver.Chrome(service=s, chrome_options=chrome_options)
위 코드를 아래처럼 수정해주니 잘 돌아간다.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(("lang=ko_KR"))
driver = webdriver.Chrome(service=s, options=chrome_options)
그래서 최종적으로 작성한 테스트 코드는
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
base_url = "https://www.google.co.kr/"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(("lang=ko_KR"))
options = webdriver.ChromeOptions()
options.add_argument('--headless')
s = Service('C:\\chromedriver_win32\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=chrome_options)
driver.get(base_url)
driver.implicitly_wait(5)
driver.get_screenshot_as_file('google_screen.png')
driver.close()
덕분에 해결했습니다.감사합니다.