Files
fg-auto/data/weather_capture.py
KWON fb7447d44e StaleElementReferenceException 발생 시 5번까지 재시도하며 에러 로그 출력.
TimeoutException도 재시도에 포함.

finally 블록으로 driver.quit() 보장.

옵션 및 임시 유저 데이터 디렉토리 지정으로 충돌 방지.

WebDriverWait + element_to_be_clickable 로 클릭 안정성 향상.
2025-06-27 12:29:43 +09:00

75 lines
2.6 KiB
Python

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
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.common.exceptions import StaleElementReferenceException, TimeoutException
from datetime import datetime
import os
import time
import tempfile
# 크롬 옵션 설정
options = Options()
options.add_argument('--headless')
options.add_argument('--window-size=1802,1467')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
# 임시 사용자 데이터 디렉토리 생성 및 지정 (중복 실행 문제 방지)
temp_dir = tempfile.mkdtemp()
options.add_argument(f'--user-data-dir={temp_dir}')
driver = webdriver.Chrome(options=options)
try:
script_dir = os.path.dirname(os.path.abspath(__file__))
driver.get('https://www.weather.go.kr/w/weather/forecast/short-term.do#dong/4148026200/37.73208578534846/126.79463099866948')
wait = WebDriverWait(driver, 10)
# 첫 번째 탭 클릭 (안전하게 클릭 대기)
tab_button = wait.until(EC.element_to_be_clickable(
(By.XPATH, '//*[@id="digital-forecast"]/div[1]/div[3]/div[1]/div/div/a[2]')
))
tab_button.click()
# 두 번째 항목 클릭 - stale element 대비 최대 5회 재시도
list_button_xpath = '//*[@id="digital-forecast"]/div[1]/div[3]/ul/div[1]/a[2]'
for attempt in range(5):
try:
list_button = wait.until(EC.element_to_be_clickable((By.XPATH, list_button_xpath)))
list_button.click()
break
except StaleElementReferenceException:
print(f"시도 {attempt+1}: stale element 참조 오류 발생, 재시도 중...")
time.sleep(1)
except TimeoutException:
print(f"시도 {attempt+1}: 요소 대기 시간 초과, 재시도 중...")
time.sleep(1)
else:
print("두 번째 항목 클릭 실패. 스크립트 종료.")
driver.quit()
exit(1)
time.sleep(2) # 페이지 반영 대기
# 캡처 대상 요소 대기 후 찾기
target_element = wait.until(EC.presence_of_element_located(
(By.XPATH, '/html/body/div[2]/section/div/div[2]')
))
# 저장 경로 설정
timestamp = datetime.now().strftime('%Y%m%d')
save_path = os.path.join(script_dir, f'weather_capture_{timestamp}.png')
# 요소 스크린샷 저장
target_element.screenshot(save_path)
print(f'📸 캡처 완료! 저장 위치: {save_path}')
finally:
driver.quit()