직장인

[회사원] 업무 코딩기록 - (Selenium)웹페이지 iframe검색

cyy1211 2024. 12. 26. 09:18
728x90
반응형
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager  # Chrome driver 자동 업데이트
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import TimeoutException
import time

# 브라우저 꺼짐 방지
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"])

# Chrome driver Manager를 통해 크롬 드라이버 자동 설치
service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)

driver.implicitly_wait(10)  # 웹페이지 로딩 될 때까지 10초 대기
driver.maximize_window()  # 화면 최대화

# 크롬 드라이버에 url 주소 넣고 실행

# 페이지가 완전히 로딩될 때까지 대기
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="TextUserID"]')))

# 검색어 입력
search_box = driver.find_element('xpath', '//*[@id="TextUserID"]')
search_box.send_keys('사번기입')

# 비밀번호 입력
password_box = driver.find_element('xpath', '//*[@id="TextPassword"]')
password_box.send_keys('비번기입')
password_box.send_keys(Keys.RETURN)

# 마이오피스 버튼이 위치한 topFrame으로 전환
# driver.switch_to.frame("topFrame")

# 마이오피스 버튼('//*[@id="top_menu02(18)"]') 클릭을 위한 대기
#try:
    # 대기 시간 증가
    #WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//*[@id="top_menu02(18)"]')))
   
    # 버튼 요소 가져오기
    #Office_btn = driver.find_element(By.XPATH, '//*[@id="top_menu02(18)"]')

    # 버튼이 가시적이고 클릭 가능한지 확인
    #if Office_btn.is_displayed() and Office_btn.is_enabled():
        # JavaScript로 클릭 시도
        #driver.execute_script("arguments[0].click();", Office_btn)
    #else:
        #print("버튼이 표시되지 않거나 클릭할 수 없습니다.")
#except TimeoutException:
    #print("버튼을 찾는 데 시간이 초과되었습니다.")
#except Exception as e:
    #print(f"예외 발생: {e}")
     
# 웹페이지 내 모든 iframe의 ID 추출
iframes = driver.find_elements(By.TAG_NAME, 'iframe')
print(f'iframe 개수: {len(iframes)}')
for frame in iframes:
    print(f'name: {frame.get_attribute("name")}')
    print(f'id: {frame.get_attribute("id")}\n')    
   
# 웹페이지가 닫히지 않도록 사용자 입력 대기
input("웹페이지를 닫으려면 Enter 키를 누르세요...")

# 드라이버 종료
driver.quit()
728x90
반응형