-
[PYTHON / SELENIUM 에러 처리] NoSuchWindowException 에러 처리python 에러 2024. 4. 11. 10:24
에러 발생 원인
selenium 은 기본적으로 사람이 하는 동작처럼 웹 사이트를 열고, 해당하는 동작을 클릭하는 것을 반복문 등을 통해서 크롤링하는 것이다.
그래서 웹 사이트가 열리기 전에 클릭을 하면 에러가 생기기에, 적절한 간격을 두고 클릭을 하게끔 만들어 주면 된다.
<selenium 새 창 로딩될 때까지 기다리기>
from selenium.common.exceptions import NoSuchWindowException from selenium.webdriver.support.ui import WebDriverWait def found_window(name): def predicate(driver): try: driver.switch_to_window(name) except NoSuchWindowException: return False else: return True # found window return predicate driver.find_element_by_id("id of the button that opens new window").click() WebDriverWait(driver, timeout=50).until(found_window("new window name")) WebDriverWait(driver, timeout=10).until( # wait until the button is available lambda x: x.find_element_by_id("id of button present on newly opened window"))\\ .click()
'python 에러' 카테고리의 다른 글
[python / Mac] "zsh: command not found: jupyter" 에러 해결 (0) 2024.03.16