ChromeDriver를 활용해 크롤링을 구현해놨는데 오늘 처음보는 에러가 나왔다.
내용을 읽어보니 chrome_options 라는 argument 가 안맞는 듯 해 혹시하고 Selenium 공식 사이트를 확인해 보았다.
get_upcoming_items...
__init__() got an unexpected keyword argument 'chrome_options'
finally...
Traceback (most recent call last):
File "/home/runner/work/ShoesPlease/ShoesPlease/parser/get_upcoming_items.py", line 23, in get_upcoming_items
driver = webdriver.Chrome('chromedriver', chrome_options=options)
TypeError: __init__() got an unexpected keyword argument 'chrome_options'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/runner/work/ShoesPlease/ShoesPlease/parser/get_upcoming_items.py", line 57, in get_upcoming_items
driver.quit()
UnboundLocalError: local variable 'driver' referenced before assignment
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "run.py", line 8, in <module>
upcoming_items = get_upcoming_items() # upcoming 탭의 모든 아이템
File "/home/runner/work/ShoesPlease/ShoesPlease/parser/get_upcoming_items.py", line 61, in get_upcoming_items
driver.quit()
UnboundLocalError: local variable 'driver' referenced before assignment
Error: Process completed with exit code 1.
원인
https://www.selenium.dev/blog/2023/selenium-4-10-0-released/
selenium이 23.06.07 일자로 4.10.0 으로 업데이트가 되었다.
몇몇 클래스의 인자가 변경되었는데, 그 중에 webdriver.Chrome() 의 인자 중 'chrome_options' 가 'options' 로 변경된게 문제였다.
대응
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
from webdriver_manager.chrome import ChromeDriverManager
options = ChromeOptions()
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
options.add_argument('user-agent=' + user_agent)
options.add_argument("lang=ko_KR")
options.add_argument('headless')
options.add_argument('window-size=1920x1080')
options.add_argument("disable-gpu")
options.add_argument("--no-sandbox")
# 크롬 드라이버 최신 버전 설정
service = ChromeService(executable_path=ChromeDriverManager().install())
# chrome driver
driver = webdriver.Chrome(service=service, options=options) # <- options로 변경