selenium之预期条件判断方法

expected_conditions 类提供的预期条件判断方法

一. title_is

判断当前页面的标题是否等于预期
expected_conditions.title_is(“预期的网页标题全部内容”)(driver)
源码:

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."""
    def __init__(self, title):
        self.title = title

    def __call__(self, driver):
        return self.title == driver.title

实例:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
res = expected_conditions.title_is("百度一下,你就知道")(driver)
print(res)

二. title_contains

判断当前页面的标题是否包含预期字符串

三. presence_of_element_located

判断元素是否被加在 DOM 树里,并不代表该元素一定可见;
返回对应的元素对象;
需要导入:from selenium.webdriver.common.by import By

源码:

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)

def _find_element(driver, by):
    """Looks up an element. Logs and re-raises ``WebDriverException``
    if thrown."""
    try:
        return driver.find_element(*by)
    except NoSuchElementException as e:
        raise e
    except WebDriverException as e:
        raise e

实例:

# 返回的res是这个元素;如果没这个元素则抛异常
res = expected_conditions.presence_of_element_located((By.XPATH ,"//input[@id='kw']"))(driver)
print(res)

四. visibility_of_element_located

判断元素是否可见(可见代表元素非隐藏,并且元素的宽和高都不等
于 0)

五. visibility_of

与上一个方法作用相同,上一个方法的参数为定位,该方法接收的参
数为定位后的元素

六. presence_of_all_elements_located

判断是否至少有一个元素存在于 DOM 树中。例如,在页面中有 n 个元
素的 class 为“wp”,那么只要有一个元素存在于 DOM 树中就返回 True

七. text_to_be_present_in_element

判断某个元素中的 text 是否包含预期的字符串

八. text_to_be_present_in_element_value

判断某个元素的 value 属性是否包含预期的字符串

九. frame_to_be_available_and_switch_to_it

判断该表单是否可以切换进去,如果可以,返回 True 并且切换进去,
否则返回 False

十. invisibility_of_element_located

判断某个元素是否不在 DOM 树中或不可见

十一. element_to_be_clickable

判断某个元素是否可见并且是可以点击的

十二. staleness_of

等到一个元素从 DOM 树中移除

十三. element_to_be_selected

判断某个元素是否被选中,一般用在下拉列表中

十四. element_selection_state_to_be

判断某个元素的选中状态是否符合预期

十五. element_located_selection_state_to_be

与上一个方法作用相同,只是上一个方法参数为定位后的元素,该方
法接收的参数为定位

十六. alert_is_present

判断页面上是否存在 alert

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值