본문 바로가기
Python/Selenium

Selenium Tried to get X error: Element not found 해결하기

by PySun 2025. 1. 20.
반응형

소개

Selenium을 사용하면서 'ElementNotInteractableException: Element not found'라는 오류가 터지는 경우가 종종 있습니다. 이 오류는 Selenium이 특정 웹 페이지에서 원하는 요소를 찾을 수 없을 때 발생합니다. 이 블로그 글에서는 이 에러의 원인과 해결 방법에 대해 살펴보겠습니다.

에러 발생 예시 코드

먼저, 'ElementNotInteractableException' 오류가 발생할 수 있는 간단한 예시 코드를 보겠습니다.

from selenium import webdriver
from selenium.webdriver.common.by import By

# 웹 드라이버 초기화
driver = webdriver.Chrome()

# 웹사이트 열기
driver.get("https://example.com")

# 존재하지 않는 요소에 접근 시도
element = driver.find_element(By.XPATH, '//input[@id="nonexistent"]')
element.click()

에러 해결 방법

1. 요소가 페이지에 로드되었는지 확인

때로는 DOM이 완전히 로드되기 전에 요소를 찾으려 할 때 이 오류가 발생합니다. 이를 해결하기 위해 WebDriverWait을 사용하여 요소가 로드될 때까지 대기할 수 있습니다.

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://example.com")

# 요소가 로드될 때까지 대기
element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//input[@id="nonexistent"]'))
)
element.click()

2. 속성이 변경되었거나 오타 확인

해당 요소의 ID, 클래스 이름, XPath 등이 정확한지 다시 한번 확인하세요. 때로는 개발자에 의해 요소 속성이 변경되었을 수도 있습니다.

from selenium import webdriver

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

# 요소의 속성 확인
try:
    element = driver.find_element(By.ID, 'nonexistent')
    element.click()
except Exception as e:
    print(f"오류: {e}. 요소의 ID가 정확한지 확인하세요.")

마무리

이번 블로그에서는 Selenium에서 'ElementNotInteractableException: Element not found' 오류에 대해 이야기했습니다. 요소가 페이지에 로드될 때까지 기다리거나, 요소 속성을 재검토하는 것이 이 문제를 해결하는 데 큰 도움이 됩니다. 항상 페이지 구조의 변화를 주의 깊게 살펴보며 코드를 작성하는 것이 중요합니다. Selenium을 사용할 때는 오류를 피하기 위해 신중하고 체계적으로 접근하세요!

반응형