zoukankan      html  css  js  c++  java
  • Selenium

    一、sleep () 休眠方法   --time 固定等待

    在开发自动化框架过程中,最忌讳使用Python自带模块的time的sleep方法进行等待,虽然可以自定义等待时间,但当网络条件良好时,

    依旧按照预设定的时间继续等待,导致整个项目的自动化时间无限延长。不建议使用。

    二、implicitly_wait(隐式等待)

    隐式等待是通过一定的时长等待页面上某元素加载完成。如果超出了设置的时长元素还没有被加载,则抛出NoSuchElementException 异常。

    implicitly_wait 方法来实现隐式等,默认设置为0 

    隐式等待会作用于整个driver 周期,所以只需要写到get 后面即可

    三、WebDriverWait(显示等待)

    显示等待使WebDriver 等待某个条件成立时继续执行,否则在达到最长时长时抛出超时异常(TimeoutException)

    源代码参考:

    import time
    from selenium.common.exceptions import NoSuchElementException
    from selenium.common.exceptions import TimeoutException

    POLL_FREQUENCY = 0.5 # How long to sleep inbetween calls to the method
    IGNORED_EXCEPTIONS = (NoSuchElementException,) # exceptions ignored during calls to the method


    class WebDriverWait(object):
    def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
    """Constructor, takes a WebDriver instance and timeout in seconds.

    :Args:
    - driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
    - timeout - Number of seconds before timing out
    - poll_frequency - sleep interval between calls
    By default, it is 0.5 second.
    - ignored_exceptions - iterable structure of exception classes ignored during calls.
    By default, it contains NoSuchElementException only.

    Example:
    from selenium.webdriver.support.ui import WebDriverWait
    element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId"))
    is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).
    until_not(lambda x: x.find_element_by_id("someId").is_displayed())
    """
    self._driver = driver
    self._timeout = timeout
    self._poll = poll_frequency

    driver :浏览器驱动
    timeout :最长超时时间,默认以秒为单位
    poll_frequency :检测的间隔(步长)时间,默认为0.5S
    ignored_exceptions :超时后的异常信息,默认情况下抛出 NoSuchElementException异常
    WebDriverWait()  一般有until() 或 until_not() 方法配合使用
    引入方法:
    from selenium.webdriver.support.wait import  WebDriverWait
    from selenium.webdriver.support import expected_conditions as Ec

    driver.find_element_by_css_selector('xx')
    # WebDriverWait(driver,10).until(Ec.title_is('xx')) #等什么出现则继续执行
    # WebDriverWait(driver,10).until_not() #等什么消失则继续执行,不常用
    # e = WebDriverWait(driver,10).until(Ec.presence_of_element_located(('id','i1')))#等什么出现则继续执行
    expected_conditions 类提供的预期条件判断方法如下:
    1 title_is: 判断当前页面的title是否完全等于(==)预期字符串,返回布尔值
    2 title_contains : 判断当前页面的title是否包含预期字符串,返回布尔值
    3 presence_of_element_located : 判断某个元素是否被加到了dom树里,并不代表该元素一定可见
    4 visibility_of_element_located : 判断某个元素是否可见. 可见代表元素非隐藏,并且元素的宽和高都不等于0
    5 visibility_of : 跟上面的方法做一样的事情,只是上面的方法要传入locator,这个方法直接传定位到的element就好了
    6 presence_of_all_elements_located : 判断是否至少有1个元素存在于dom树中。举个例子,如果页面上有n个元素的class都是‘column-md-3‘,
    那么只要有1个元素存在,这个方法就返回True 7 text_to_be_present_in_element : 判断某个元素中的text是否 包含 了预期的字符串 8 text_to_be_present_in_element_value : 判断某个元素中的value属性是否 包含 了预期的字符串 9 frame_to_be_available_and_switch_to_it : 判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False 10 invisibility_of_element_located : 判断某个元素中是否不存在于dom树或不可见 11 element_to_be_clickable : 判断某个元素中是否可见并且是enable的,这样的话才叫clickable 12 staleness_of : 等某个元素从dom树中移除,注意,这个方法也是返回True或False 13 element_to_be_selected : 判断某个元素是否被选中了,一般用在下拉列表 14 element_selection_state_to_be : 判断某个元素的选中状态是否符合预期 15 element_located_selection_state_to_be : 跟上面的方法作用一样,只是上面的方法传入定位到的element,而这个方法传入locator 16 alert_is_present : 判断页面上是否存在alert


  • 相关阅读:
    睡前一分钟打造完美下半身 健康程序员,至尚生活!
    几种不伤身体的速效减肥秘方 健康程序员,至尚生活!
    头发一周洗几次才适宜? 健康程序员,至尚生活!
    夏日驱蚊虫蟑螂的最好办法! 健康程序员,至尚生活!
    WPF控件和布局
    《深入浅出WPF》笔记——绑定篇(二)
    WPF中的DataTemplate绑定使用的场合
    WPF第一个程序和XAML初探
    实习总结之jquery实例
    下一步要实战的东西
  • 原文地址:https://www.cnblogs.com/chendai21/p/8961999.html
Copyright © 2011-2022 走看看