QA Engineering/Tool & Automation

셀레니움 웹 페이지 로딩이 너무 길어 질때

일해라폴폴 2023. 11. 18. 12:28
반응형

제곧내 인데...
특정 페이지에 접속 했을때, Loading 바가 1분 넘게 돌아 가는 경우가 있다
찾으려는 Element 가 없어서 그런것 같지는 않고 그냥 페이지 로딩이 길어 지는 것 같아서
일단은 아래 방법으로 로딩 멈춤을 해보려고 한다
단점은... 아직 적용을 안해 봐서 모름 ㅎㅎ

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
from selenium.common.exceptions import TimeoutException, NoSuchElementException

MAX_LOAD_TIME = 5 # 최대 페이지 로딩 시간 설정 (5초)

driver = webdriver.Chrome()
driver.set_page_load_timeout(MAX_LOAD_TIME)

try:
    driver.get("https://mkpark01.tistory.com/677") # 문제의 티스토리;ㅁ;

    button_class_name = "your-button-class" # 임의로 클릭버튼 누른다는 가정 하에, MAX_LOAD_TIME 적용
    WebDriverWait(driver, MAX_LOAD_TIME).until(
        EC.element_to_be_clickable((By.CLASS_NAME, button_class_name))
    )

    button = driver.find_element(By.CLASS_NAME, button_class_name)
    button.click()

except TimeoutException:
    print("Page or button took too long to load. Stopping the script.")

except NoSuchElementException:
    print(f"Button with class name '{button_class_name}' not found on the page.")

finally:
    driver.quit()

 

 

반응형