본문 바로가기

Python

Selenium 에서 "display: none" 속성 element의 text 가져오기

Selenium에서 python으로 CSS Selector를 이용해서 날짜 시간 정보인 Text를 가져온다고 하면 아래처럼 하면 된다.

<span class="timestamp">2023-01-01 19:54:36</span>
date = item.find_element(By.CSS_SELECTOR, "span.timestamp").text

 

그런데 .timestamp 의 display 속성이 아래처럼 숨겨져 있다면 위의 코드로는 text를 가져올 수 없다.

.timestamp, .ip_address, .sn_num {
    display: none;

이런 경우 아래처럼 처리해주면 된다.

date = item.find_element(By.CSS_SELECTOR, "span.timestamp").get_attribute('textContent')