소개
Selenium을 사용하여 웹 자동화를 수행할 때 'ElementNotSelectableException' 오류를 마주치는 것은 꽤 당혹스러운 경험일 수 있습니다. 이 오류는 드롭다운이나 체크박스와 같은 UI 요소를 선택하려고 할 때 발생하며, 해당 요소가 선택 불가능한 상태일 때 발생합니다. 이번 블로그 글에서는 이 문제의 원인과 해결 방법에 대해 알아보겠습니다.
에러 발생 예시 코드
먼저 'ElementNotSelectableException' 오류가 발생할 수 있는 간단한 예시 코드를 살펴보겠습니다.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import ElementNotSelectableException
# Chrome 웹 드라이버 시작
driver = webdriver.Chrome()
# 특정 웹 페이지로 이동
driver.get("http://example.com/dropdown")
# 드롭다운 메뉴 선택 시도
try:
dropdown = Select(driver.find_element_by_id("dropdown"))
dropdown.select_by_visible_text("선택 불가능한 옵션")
except ElementNotSelectableException as e:
print(f"오류 발생: {e}")
# 드라이버 종료
driver.quit()
에러 해결 방법
1. 선택 가능한 옵션 확인
먼저, 선택하려는 옵션이 실제로 선택 가능한 상태인지 확인해야 합니다. 웹 페이지의 HTML 구조를 통해 해당 옵션이 비활성화(disable) 상태인지 점검하세요.
options = dropdown.options
for option in options:
print(f"옵션: {option.text}, 선택 가능: {option.is_enabled()}")
2. 드롭다운 상태 확인
드롭다운이 활성화되어 있는지 확인하여 비활성화 상태인 경우 관련 응답이 완료될 때까지 기다릴 수 있습니다.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "dropdown")))
# 드롭다운 선택 시도
dropdown.select_by_visible_text("선택 가능한 옵션")
3. JavaScript로 선택하기
일부 경우, Selenium만으로는 UI 요소를 제대로 조작할 수 없는 경우도 있습니다. 이때, JavaScript를 활용하여 직접 선택할 수 있습니다.
driver.execute_script("arguments[0].selected = true;", dropdown.options[0])
마무리
이 블로그 글에서는 Selenium에서 발생하는 'ElementNotSelectableException' 오류를 해결하기 위한 다양한 방법을 살펴보았습니다. 선택 가능한 옵션 확인, 드롭다운 상태 확인, 그리고 JavaScript 활용 방법까지 다양한 접근법을 통해 이 오류를 극복할 수 있습니다. Selenium을 활용한 웹 자동화 시 항상 DOM 구조에 주의하고, 상황에 맞는 방법으로 요소에 접근하세요!
'Python > Selenium' 카테고리의 다른 글
Selenium Error executing JavaScript 오류 해결하기 (0) | 2025.01.10 |
---|---|
Selenium ElementNotSelectedException 오류 해결하기 (0) | 2025.01.10 |
Selenium.run_script로 브라우저에서 스크립트 실행하기 (0) | 2025.01.10 |
Selenium.flip으로 브라우저 세션 전환하기 (0) | 2025.01.10 |
Selenium.scroll_to로 페이지 스크롤하기 (0) | 2025.01.10 |