from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time # Chrome 드라이버 설정 driver = webdriver.Chrome() # URL에 접속 driver.get("https://www.bobaedream.co.kr/mycar/mycar_list.php?gubun=K") time.sleep(3) # 초기 로딩 대기 # 1. 제조사 정보 추출 및 클릭 manufacturers = driver.find_elements(By.CSS_SELECTOR, ".area-maker .group-list dd") for manufacturer in manufacturers: manufacturer_button = manufacturer.find_element(By.TAG_NAME, "button") manufacturer_name = manufacturer_button.find_element(By.CSS_SELECTOR, "span.t1").text manufacturer_button.click() time.sleep(2) # 모델 로딩 대기 # 2. 모델 정보 추출 및 클릭 models = driver.find_elements(By.CSS_SELECTOR, ".area-model .group-list dd") for model in models: model_button = model.find_element(By.TAG_NAME, "button") model_name = model_button.find_element(By.CSS_SELECTOR, "span.t1").text model_button.click() time.sleep(2) # 세부모델 로딩 대기 # 3. 세부모델 정보 추출 및 체크박스 클릭 sub_models = driver.find_elements(By.CSS_SELECTOR, ".area-detail .group-list dd[class*='c_model_']") sub_model_found = False # 세부모델이 있는지 확인하기 위한 플래그 for sub_model in sub_models: # 'display:none' 상태를 확인하고 다음 세부모델로 이동 if "display:none" in sub_model.get_attribute("style"): continue # 가시화되지 않은 세부모델은 건너뜀 sub_model_name = sub_model.find_element(By.CSS_SELECTOR, "label").text sub_model_checkbox = sub_model.find_element(By.CSS_SELECTOR, "input[type='checkbox']") # 세부모델 체크박스가 있는지 확인 if sub_model_checkbox.is_displayed(): # 스크롤하여 체크박스가 화면에 보이도록 설정 driver.execute_script("arguments[0].scrollIntoView(true);", sub_model_checkbox) # 명시적으로 대기하여 체크박스가 인터랙티브한지 확인 try: # JavaScript로 체크박스 클릭 driver.execute_script("arguments[0].click();", sub_model_checkbox) sub_model_found = True time.sleep(2) # 세부모델 클릭 후 등급 로딩 대기 print(f"{manufacturer_name} -> {model_name} -> {sub_model_name}") # 4. 세부모델이 선택된 경우에만 등급 출력 grades = driver.find_elements(By.CSS_SELECTOR, ".area-grade .group-list dd") if grades: # 등급이 있는 경우만 출력 for grade in grades: grade_name = grade.find_element(By.CSS_SELECTOR, "label").text grade_count = grade.find_element(By.CSS_SELECTOR, "span.t2").text print(f" 등급: {grade_name}, 수량: {grade_count}") else: print(f" {sub_model_name} -> 등급 없음") # 5. 세부모델 체크 해제 (체크박스 클릭) driver.execute_script("arguments[0].click();", sub_model_checkbox) time.sleep(1) # 체크 해제 후 잠시 대기 except Exception as e: print(f" {sub_model_name} -> 클릭 불가: {e}") # 세부모델이 없으면 등급을 출력하지 않고 건너뜀 if not sub_model_found: print(f"{manufacturer_name} -> {model_name} (세부모델 없음)") time.sleep(3) # 제조사 변경 시 대기 driver.quit()