본문 바로가기

PYTHON/자동화

Implicit Waits vs explicitly wait (Expected Conditions)

728x90

동적으로 변하는 웹페이지라면 명시적대기 explicitly wait
그렇지 않다면 암묵적대기 implicitly wait (범위시간 내에서 대기)
explicit wait 더 많은 제어권 제공. 동기화가 필요한 시점에 나머지 테스트 스크립트 정상동작

공식사이트: https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions
공식사이트 wait : https://selenium-python.readthedocs.io/waits.html

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )

finally:
    driver.quit()

Expected Conditions

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'someid')))

title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
text_to_be_present_in_element
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
invisibility_of_element_located
element_to_be_clickable
staleness_of
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present

Custom Wait Conditions

class element_has_css_class(object):
  """An expectation for checking that an element has a particular css class.

  locator - used to find the element
  returns the WebElement once it has the particular css class
  """
  def __init__(self, locator, css_class):
    self.locator = locator
    self.css_class = css_class

  def __call__(self, driver):
    element = driver.find_element(*self.locator)   # Finding the referenced element
    if self.css_class in element.get_attribute("class"):
        return element
    else:
        return False

# Wait until an element with id='myNewInput' has class 'myCSSClass'
wait = WebDriverWait(driver, 10)
element = wait.until(element_has_css_class((By.ID, 'myNewInput'), "myCSSClass"))

필요한 부분만 남긴 코드

class element_has_css_class(object):
    def __init__(self, locator, css_class):
        self.locator = locator
        self.css_class = css_class

    def __call__(self, driver):
        element = driver.find_element(*self.locator)   # Finding the referenced element
        if self.css_class in element.get_attribute("class"):
            return element
        else:
            return False   
wait = WebDriverWait(driver, 10)
element = wait.until(element_has_css_class((By.ID, 'myNewInput'), "myCSSClass"))

Implicit Waits

driver.implicitly_wait(5)
from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")

'PYTHON > 자동화' 카테고리의 다른 글

selenium 속도향상 옵션추가  (0) 2019.12.19
회사컴도 과부하 걸리는..  (0) 2019.11.22
console창에서 코드save&load  (0) 2019.11.19
selenium headless chrome  (0) 2019.11.18
유튜브영상링크 스크롤 끝까지 크롤링  (0) 2019.11.17