컴퓨터/Python

셀레니움 콤보박스 모든 텍스트 가져오기

풍경소리^^ 2023. 12. 2. 23:25
from selenium import webdriver
from selenium.webdriver.support.ui import Select

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

# 웹페이지 열기
driver.get("https://example.com")  # 대상 웹페이지 주소로 변경

# 콤보박스 식별 (XPath를 사용하는 예제)
combo_box = driver.find_element_by_xpath("//select[@id='yourComboBoxId']")  # 대상 콤보박스의 id 속성으로 변경

# 셀렉트 객체 생성
select = Select(combo_box)

# 콤보박스의 모든 옵션 텍스트 가져오기
all_options = select.options

# 각 옵션의 텍스트 출력
for option in all_options:
    print(option.text)

# 웹드라이버 종료
driver.quit()