在上一章python 爬虫前奏五 webdriver中的等待中简单的提过WebDriverWait与expected_conditions结合使用,但是这个是需要详细了解的
内容来源于互联网,这里仅做笔记和参考用
一、Expected Conditions 用法详解
首先添加包
from selenium.webdriver.support import expected_conditions as EC
1、判断当前页面的 title 是否等于预期值
title_is( String title)
2、判断当前页面的 title 是否包含预期值
title_contains( String title)
3、判断当前页面的url是否等于预期值
url_to_be( String url)
4、判断当前页面的url是否包含预期字符串
url_contains( String fraction)
5、字符串正则表达式
url_matches( String regex)
6、判断元素是否出现,只要有一个元素出现,就通过。
presence_of_element_located( By locator)
7、判断元素是否出现,必须所有符合条件的元素都加载出来,才通过。
presence_of_elements_located( By locator)
8、如果给定元素是可见的且具有非零大小,否则为null
element_if_visible(WebElement element)
9、判断元素是否出现。
presence_of_all_elements_located_by( By locator)
10、传入类型为:locator 判断某个元素是否可见
visibility_of_element_located( By locator)
11、判断某组元素是否可见
visibility_of_all_elements_located_by( By locator)
12、传入类型为:element 判断某个元素是否可见.
visibility_of_all_elements(final List<WebElement> elements)
13、判断某个元素中的text是否包含了预期的字符串;
text_to_be_present_in_element( WebElement element, String text)
14、判断某个元素中的 text 是否 包含 了预期的字符串
text_to_be_present_in_element(By locator, String text)
15、判断某个元素中的 text 是否 包含 了预期的字符串
text_to_be_present_in_element_located(final By locator, final String text)
16、判断某个元素中的 value 属性是否包含 了预期的字符串
text_to_be_present_in_element_value( WebElement element, String text)
17、判断某个元素中的 value 属性是否包含 了预期的字符串
text_to_be_present_in_element_value(final By locator, final String text)
18、判断该 frame 是否可以 switch进去
frame_to_be_available_and_switch_to_it(final String frameLocator)
19、判断该 frame 是否可以 switch进去
frame_to_be_available_and_switch_to_it(final By locator)
20、某个元素中是否不存在于dom树或不可见;
invisibility_of_element_located(final By locator)
21、判断带有文本的元素要么不可见,要么文本不存在于元素上
invisibility_of_element_with_text(final By locator, final String text)
22、判断某个元素中是否可见并且是enable的,这样的话才叫clickable
element_to_be_clickable(final By locator)
23、判断某个元素中是否可见并且是enable的,这样的话才叫clickable
element_to_be_clickable(final WebElement element)
24、判断一个元素是否仍在DOM中或等待从dom中移除,传入WebElement对象,可以判断页面是否刷新了,注意,这个方法是返回True或False
staleness_of(final WebElement element)
25、判断一个元素是否仍在DOM中,传入WebElement对象,可以判断页面是否刷新
refreshed(final ExpectedCondition<T> condition)
26、判断某个元素是否被选中了,一般用在下拉列表
element_to_be_selected(WebElement element)
27、判断某个元素是否被选中了,一般用在下拉列表;
element_to_be_selected(By locator)
28、判断某个元素的选中状态是否符合预期,传入element
element_selection_state_to_be( WebElement element, boolean selected)
29、判断某个元素的选中状态是否符合预期,传入locator
element_selection_state_to_be(final By locator, final boolean selected)
30、判断页面上是否存在alert。
alert_is_present()
二、使用示例
#encoding:utf-8
# example of how to use https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/support/expected_conditions.py
from selenium.webdriver.support import expected_conditions as EC
dr = webdriver.Chrome()
url = 'http://www.baidu.com'
search_text_field_id = 'kw'
dr.get(url)
class ECExample(unittest.TestCase):
def test_title_is(self):
''' 判断title是否符合预期 '''
title_is_baidu = EC.title_is(u'百度一下,你就知道')
self.assertTrue(title_is_baidu(dr))
def test_titile_contains(self):
''' 判断title是否包含预期字符 '''
title_should_contains_baidu = EC.title_contains(u'百度')
self.assertTrue(title_should_contains_baidu(dr))
def test_presence_of_element_located(self):
''' 判断element是否出现在dom树 '''
locator = (By.ID, search_text_field_id)
search_text_field_should_present = EC.visibility_of_element_located(locator)
''' 动态等待10s,如果10s内element加载完成则继续执行下面的代码,否则抛出异常 '''
WebDriverWait(dr, 10).until(EC.presence_of_element_located(locator))
WebDriverWait(dr, 10).until(EC.visibility_of_element_located(locator))
self.assertTrue(search_text_field_should_present(dr))
def test_visibility_of(self):
search_text_field = dr.find_element_by_id(search_text_field_id)
search_text_field_should_visible = EC.visibility_of(search_text_field)
self.assertTrue(search_text_field_should_visible('yes'))
def test_text_to_be_present_in_element(self):
text_should_present = EC.text_to_be_present_in_element((By.NAME, 'tj_trhao123'), 'hao123')
self.assertTrue(text_should_present(dr))
@classmethod
def tearDownClass(kls):
print 'after all test'
dr.quit()
print 'quit dr'
if __name__ == '__main__':