정보/py

__init__() got an unexpected keyword argument 'executable_path'

바다♬~♪ 2025. 2. 14. 20:45
  • 발생 위치: driver = common.chromeWebdriver(path)
  • 원인: common.chromeWebdriver(혹은 Selenium WebDriver)의 초기화 메서드에 executable_path라는 인수가 전달되고 있습니다. Selenium의 webdriver.Chrome() 초기화 방식은 Selenium 4 이후 executable_path 대신 service 객체를 사용하도록 변경되었습니다.

해결방안

 

Selenium 초기화 수정:

  • 최신 Selenium 사용 시 webdriver.Chrome() 초기화에는 Service 객체를 사용해야 합니다.
  • 따라서 executable_path를 Service 객체로 대체합니다.

 

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from bs4 import BeautifulSoup as bs
import time


def getImageUrl(url):
    # ChromeDriver 경로 설정 (https://chromedriver.chromium.org/downloads)
    path = setting_info['chromePath']

    # 드라이버 초기화
    driver = None
    try:
        # Service 객체를 통해 ChromeDriver 초기화
        service = Service(executable_path=path)
        driver = webdriver.Chrome(service=service)

        driver.get(url)
        time.sleep(10)

        # 페이지 소스 가져오기 및 BeautifulSoup 초기화
        html = driver.page_source
        soup = bs(html, 'html.parser')

        # 상세 페이지의 a 태그 정보 추출
        a_tags = soup.find_all("a", class_="se-module-image-link")

        return a_tags

    except Exception as e:
        print(f"An error occurred: {e}")

    finally:
        if driver:
            # 드라이버가 초기화된 경우에만 종료
            driver.quit()