파이썬 크롬 웹 크롤링 갑작스런 호출 오류 해결방법

실행파일로 만들어 잘 사용하던 파이썬 크롬 웹 크롤링 호출에 오류가 발생하여, 정보를 확인한 결과 버전이 업데이트 되면서 크롬의 Webdriver가 호출이 불가능하다는 것을 알게 되었습니다. 수정이 필요한 코드와 대처법에 대해서 알려드려요.

크롬-웹크롤링-오류-해결방법-썸네일


selenium 버전을 4.11.2 다운 그레이드 필요

우선 코드를 변경하시기 전에 selenium 버전을 변경하셔야 해요. 그 코드를 아무리 변경하신다고 한들, 크롬 웹드라이버를 호출할 수 는 없더라고요. pip 명령어는 다들 아실거라고 생각하지만, 다음과 같아요.


pip install selenium==4.11.2


웹드라이버 호출 코드 변경

크롬 웹드라이버 호출 코드도 변경하셔야 해요. 기존에 코딩하셨던 코드를 아래와 같이 바꾸셔야 하고 라이브러리 선언하실 때에도 import 부분을 아래와 같이 추가해 주시면 돼요.

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.action_chains import ActionChains

from webdriver_manager.chrome import ChromeDriverManager

from selenium.webdriver.chrome.service import Service

………………..

# selenium 4.x 버전 사용 시  

options = webdriver.ChromeOptions()

options.add_argument(“–start-maximized”) # 창 크기 최대화

options.add_experimental_option(‘excludeSwitches’, [‘enable-logging’])

capabilities = options.to_capabilities()

capabilities[‘screenResolution’] = ‘1920×1080’ # 모니터 해상도 설정

………………….

    service = Service()

    options = webdriver.ChromeOptions()

    driver = webdriver.Chrome(service=service, options=options)

버전이 바뀌더라도 오류가 나지 않게 코딩하는 게 가장 중요하더라고요. 간단한 크롬 웹 크롤링 코드를 작성해서 사용할 때, 가장 큰 문제는 외부 프로그램을 불러올 때 오류가 가장 많은 것 같습니다. 코드를 위와 같이 변경하셔서 실행 파일을 만들어서 사용하시면, 오류는 제거 하실 수 있을거라 생각합니다.