Selenium Python 三种等待方式

一、强制等待(sleep):

作用:程序等待seconds秒在继续执行。

优点:使用简单。

缺点:时间长短不好把握,不智能,过短程序已就报错,过长浪费执行时间。

import time

time.sleep(seconds)

二、隐式等待(implicitly_wait()):

作用:根据网页加载是否完成来等待,最长等待seconds秒,即:< seconds秒内网页加载完成,程序立即继续执行,> seconds秒网页未加载完成,程序报错退出。

优点:全局性的,在开头设置过之后,整个的程序运行过程中都会等待页面加载完成,而不需要每次设置一遍。

缺点:不是很智能,由于ajax技术的广泛应用,页面的元素都可以实现局部加载,即在整个页面没有加载完的时候,我们需要的元素可能已经加载完成,那就么有必要再等待整个页面的加载。

driver = webdriver.Firefox()
driver.get("https://www.baidu.com")
driver.implicitly_wait(seconds)

三、显式等待(WebDriverWait):

说明:

1、显示等待:WebDriverWait()

WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)
  • driver:浏览器驱动
  • timeout:最长超时时间,默认以秒为单位
  • poll_frequency:检测的间隔步长,默认为0.5s
  • ignored_exceptions:超时后的抛出的异常信息,默认抛出NoSuchElementExeception异常。

2、until()或者until_not()方法的使用:

# 直到满足condition返回True
WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None).until(condition,message="")
# 直到满足condition返回不是True
WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None).until_not(condition,message="")

3、excepted_conditions类提供的预期条件判断的方法:

方法 说明
title_is 判断当前页面的 title 是否完全等于(==)预期字符串,返回布尔值
title_contains 判断当前页面的 title 是否包含预期字符串,返回布尔值
presence_of_element_located 判断某个元素是否被加到了 dom 树里,并不代表该元素一定可见
visibility_of_element_located 判断元素是否可见(可见代表元素非隐藏,并且元素宽和高都不等于 0
visibility_of 同上一方法,只是上一方法参数为locator,这个方法参数是 定位后的元素
presence_of_all_elements_located 判断是否至少有 1 个元素存在于 dom 树中。举例:如果页面上有 n 个元素的 class 都是’wp’,那么只要有 1 个元素存在,这个方法就返回 True
text_to_be_present_in_element 判断某个元素中的 text 是否 包含 了预期的字符串
text_to_be_present_in_element_value 判断某个元素中的 value 属性是否包含 了预期的字符串
frame_to_be_available_and_switch_to_it 判断该 frame 是否可以 switch进去,如果可以的话,返回 True 并且 switch 进去,否则返回 False
invisibility_of_element_located 判断某个元素中是否不存在于dom树或不可见
element_to_be_clickable 判断某个元素中是否可见并且可点击
staleness_of 等某个元素从 dom 树中移除,注意,这个方法也是返回 True False
element_to_be_selected 判断某个元素是否被选中了,一般用在下拉列表
element_selection_state_to_be 判断某个元素的选中状态是否符合预期
element_located_selection_state_to_be 跟上面的方法作用一样,只是上面的方法传入定位到的 element,而这个方法传入 locator
alert_is_present 判断页面上是否存在 alert

优点:灵活,智能……

缺点:应该就是没有上面两种方法使用方便。

import logging
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.common.exceptions import TimeoutException

# 等待页面加载,即页面中的加载等待旋转框消失为止
def wait_until_not(self, *selector):

    element = WebDriverWait(self.driver, 10, 0.1, ignored_exceptions=TimeoutException).until_not(
        ec.presence_of_element_located(*selector)
        )
    logging.info("The waiting element is %s, the end." % element)


# 等待页面加载,即页面中的加载等待旋转框出现为止
def wait_until_not(self, *selector):

    element = WebDriverWait(self.driver, 10, 0.1, ignored_exceptions=TimeoutException).until(
        ec.presence_of_element_located(*selector)
        )
    logging.info("The waiting element is %s, the end." % element)

4、与匿名函数lambda结合使用:

    # 判断元素是否存在
    def is_exists(self, locator):
        try:
            WebDriverWait(self.driver, 15, 0.1).until(lambda x: x.find_element(*locator))
            return True
        except NoSuchElementException:
            return False

    # 等待页面加载,即页面中的加载等待旋转框消失为止
    def is_disappeared(self, *selector, timeout=10):

        element = WebDriverWait(self.driver, timeout, 0.1, ignored_exceptions=TimeoutException).until_not(
            lambda x: x.find_element(*selector).is_displayed()
        )
        logging.info("The waiting element is %s, the end." % element)
发布了30 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/u014722022/article/details/102802192