zoukankan      html  css  js  c++  java
  • 【转】selenium webdriver三种等待方法

    原文:https://www.cnblogs.com/lgh344902118/p/6015593.html

    webdriver三种等待方法

    1.使用WebDriverWait

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait                            # available since 2.4.0
    from selenium.webdriver.support import expected_conditions as EC           # available since 2.26.0
    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()

    presence_of_element_located可以替换为

    • 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 - it is Displayed and Enabled.
    • 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

    By.ID中的ID可替换为'CLASS_NAME', 'CSS_SELECTOR', 'ID', 'LINK_TEXT', 'NAME', 'PARTIAL_LINK_TEXT', 'TAG_NAME', 'XPATH'

    2、隐式等待,相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。

    隐式等待使得WebDriver在查找一个Element或者Element数组时,每隔一段特定的时间就会轮询一次DOM,如果Element或数组没有马上被发现的话。

    默认设置是0。

    一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用。

    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.implicitly_wait(10)                      # seconds
    driver.get(http://www.xxx.com)
    myDynamicElement = driver.find_element_by_id("ElementID")

    3、强制等待

    import time
    
    time.sleep(10)
  • 相关阅读:
    BZOJ2219数论之神——BSGS+中国剩余定理+原根与指标+欧拉定理+exgcd
    Luogu 3690 Link Cut Tree
    CF1009F Dominant Indices
    CF600E Lomsat gelral
    bzoj 4303 数列
    CF1114F Please, another Queries on Array?
    CF1114B Yet Another Array Partitioning Task
    bzoj 1858 序列操作
    bzoj 4852 炸弹攻击
    bzoj 3564 信号增幅仪
  • 原文地址:https://www.cnblogs.com/eedc/p/8642786.html
Copyright © 2011-2022 走看看