标签:style blog class code c java
如今,大多数的Web应用程序使用AJAX技术。当页面加载到浏览器,页面中的元素也许在不同的时间间隔内加载。这使得元素很难定位,如果在DOM中的元素没有呈现,它将抛出ElementNotVisibleException异常。使用waits,我们可以解决这个问题。
Selenium WebDriver 提供两种类型的waits -- 隐式和显式。显式的wait使webdriver等待发生之前,继续执行一定的条件。一个隐式的wait使webdriver DOM在一定时间后,试图定位元素。
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()
这种wait最多等待10秒后会抛出一个TimeoutException异常,或者如果它在0-10s内发现该元素将返回一次。默认情况下WebDriverWait调用ExpectedCondition每500毫秒,直到它成功返回。一个ExpectedCondition类型的成功的返回类型是布尔返回true或不为null返回值。
Expected Conditions(预期条件)· 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
from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) element = wait.until(EC.element_to_be_clickable((By.ID,‘someid‘)))
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")
<译>Selenium Python Bindings 5 - Waits,布布扣,bubuko.com
<译>Selenium Python Bindings 5 - Waits
标签:style blog class code c java
原文地址:http://www.cnblogs.com/wuzhiming/p/3730004.html