Python+Selenium之expected_conditi
2018-01-15 本文已影响177人
路由心定
文件位置:(Python安装位置)E:\Python27\Lib\site-packages\selenium\webdriver\support
简介:expected_conditions一般也简称EC,咱们看着源码一个个分析用离子来分析其具体作用
1.title_is
1.1 源码
class title_is(object):
"""An expectation for checking the title of a page.
title is the expected title, which must be an exact match
returns True if the title matches, false otherwise."""
#检查页面的title与期望值是都完全一致,如果完全一致,返回Ture,否则返回Flase
def __init__(self, title):
self.title = title
def __call__(self, driver):
return self.title == driver.title
1.2 作用
检查页面的title与期望值是都完全一致,如果完全一致,返回True,否则返回Flase
1.3 实例用法
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Firefox()
driver.get('https://www.jianshu.com/u/00578e97e968')
title=EC.title_is(u'路由心定 - 简书')
print title(driver)
2.title_contains
2.1 源码
class title_contains(object):
""" An expectation for checking that the title contains a case-sensitive
substring. title is the fragment of title expected
returns True when the title matches, False otherwise
"""
def __init__(self, title):
self.title = title
def __call__(self, driver):
return self.title in driver.title
2.2 作用
作用与title_is
差不多,只不过这个是只要包含指定字符就可以
2.3 实例用法
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Firefox()
driver.get('https://www.jianshu.com/u/00578e97e968')
title1=EC.title_is(u'路由心定 - 简书')
title2=EC.title_contains(u'路由心定')
print title1(driver)
print title2(driver)
3.presence_of_element_located
3.1 源码
class presence_of_element_located(object):
""" An expectation for checking that an element is present on the DOM
of a page. This does not necessarily mean that the element is visible.
locator - used to find the element
returns the WebElement once it is located
"""
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
return _find_element(driver, self.locator)
3.2 作用
判断某个元素是否被加到了dom树里,并不代表该元素一定可见 ,通俗易懂点就是元素存不存在这个页面
3.3 实际用法
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
driver=webdriver.Firefox()
driver.get('https://www.jianshu.com/u/00578e97e968')
elem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div[1]/div[1]/div[1]/a")))
print elem
欢迎关注微信公众平台:我要学测试