본문 바로가기
Python/Selenium

Selenium ScreenShotException 오류 해결하기

by PySun 2025. 1. 7.
반응형

소개

Selenium을 사용하다 보면 'ScreenShotException' 오류에 직면할 때가 종종 있습니다. 이 오류는 웹 페이지의 스크린샷을 캡처하려 할 때 발생할 수 있으며, 다양한 원인이 있을 수 있습니다. 이번 블로그 글에서는 'ScreenShotException' 오류의 발생 원인과 그 해결방법에 대해 알아보겠습니다.

에러 발생 예시 코드

먼저, 'ScreenShotException'이 발생할 수 있는 간단한 예시 코드를 살펴보겠습니다.

from selenium import webdriver

# Chrome 브라우저 로드
driver = webdriver.Chrome()

# 웹 페이지 열기
driver.get("https://www.example.com")

# 잘못된 스크린샷 시도
screenshot_path = "screenshot.png"
driver.get_screenshot_as_file(screenshot_path)

# 웹 페이지 종료
driver.quit()

에러 해결 방법

1. 유효한 웹 드라이버 경로 확인

때때로, 'ScreenShotException'은 유효하지 않은 웹 드라이버 경로로 인해 발생할 수 있습니다. 웹 드라이버가 올바르게 설치되어 있는지 확인하고 그 경로를 설정합니다.

from selenium import webdriver
import os

# 유효한 드라이버 경로 설정
driver_path = os.path.join(os.getcwd(), 'chromedriver')
driver = webdriver.Chrome(executable_path=driver_path)

driver.get("https://www.example.com")
driver.get_screenshot_as_file("screenshot.png")
driver.quit()

2. 페이지 로딩 완료 확인

스크린샷을 찍기 전에 웹 페이지가 완전히 로드되었는지 확인하는 것도 좋은 방법입니다. JavaScript 혹은 AJAX 요청이 완료되지 않을 경우에도 'ScreenShotException'이 발생할 수 있습니다.

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

driver = webdriver.Chrome()
driver.get("https://www.example.com")

# 페이지 로딩 완료 대기
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "h1")))

driver.get_screenshot_as_file("screenshot.png")
driver.quit()

마무리

이번 블로그 글에서는 Selenium에서 발생하는 'ScreenShotException' 오류에 대한 간단한 해결 방법을 살펴보았습니다. 유효한 웹 드라이버 경로를 확인하거나 페이지 로딩이 완료되는 것을 대기함으로써 이러한 오류를 극복할 수 있습니다. Selenium을 사용할 때는 항상 코드의 안정성을 고려하고, 디버깅 과정을 통해 발생하는 각종 오류를 해결해 나가는 것이 중요합니다.

반응형