selenium wait

来源:互联网 发布:挖机大转盘型号数据 编辑:程序博客网 时间:2024/04/30 16:01

Explicit Waits:

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0ff = webdriver.Firefox()ff.get("http://somedomain/url_that_delays_loading")try:    element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))finally:    ff.quit()

Implicit Waits:

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

from selenium import webdriverff = webdriver.Firefox()ff.implicitly_wait(10) # secondsff.get("http://somedomain/url_that_delays_loading")myDynamicElement = ff.find_element_by_id("myDynamicElement")

0 0
原创粉丝点击