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

    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)

  • 相关阅读:
    阿里云服务器mysql连接不了2003 -can't connect to mysql server on 'ip' (10038)
    androidstudio代码混淆
    apk反编译
    android bottomnavicationbar底部图标vector设计
    Java++:不要随意使用 Cookie 会引火烧身的
    Java++:用户认证-基于 jwt 和 session 的区别和优缺点
    Spring++:AOP 拦截 Service | Controller 日志
    Spring++:整合 Retry 实现重试机制
    (xxl_job | quartz):XXL_JOB 对比 Quartz 一决高下!
    Spring++:定时任务 Cron表达式详解
  • 原文地址:https://www.cnblogs.com/lgh344902118/p/6015593.html
Copyright © 2011-2022 走看看