Selenium 高阶应用之WebDriverWait 和 expected_conditions

  Seleniium 是相当不错的一个第三方测试框架,可惜目前国内已经无法访问其官网(FQ可以)。

不知道大家是否有认真查看过selenium 的api,我是有认真学习过的。selenium 的api中包含有WebDriverWait  和 expected_conditions这两个高级应用。

  下面先看WebDriverWait :

 1 import time
 2 from selenium.common.exceptions import NoSuchElementException
 3 from selenium.common.exceptions import TimeoutException
 4
 5 POLL_FREQUENCY = 0.5  # How long to sleep inbetween calls to the method
 6 IGNORED_EXCEPTIONS = (NoSuchElementException,)  # exceptions ignored during calls to the method
 7
 8
 9 class WebDriverWait(object):
10     def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
11         """Constructor, takes a WebDriver instance and timeout in seconds.
12
13            :Args:
14             - driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
15             - timeout - Number of seconds before timing out
16             - poll_frequency - sleep interval between calls
17               By default, it is 0.5 second.
18             - ignored_exceptions - iterable structure of exception classes ignored during calls.
19               By default, it contains NoSuchElementException only.
20
21            Example:
22             from selenium.webdriver.support.ui import WebDriverWait \n
23             element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
24             is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
25                         until_not(lambda x: x.find_element_by_id("someId").is_displayed())
26         """
27         self._driver = driver
28         self._timeout = timeout
29         self._poll = poll_frequency
30         # avoid the divide by zero
31         if self._poll == 0:
32             self._poll = POLL_FREQUENCY
33         exceptions = list(IGNORED_EXCEPTIONS)
34         if ignored_exceptions is not None:
35             try:
36                 exceptions.extend(iter(ignored_exceptions))
37             except TypeError:  # ignored_exceptions is not iterable
38                 exceptions.append(ignored_exceptions)
39         self._ignored_exceptions = tuple(exceptions)
40
41     def until(self, method, message=‘‘):
42         """Calls the method provided with the driver as an argument until the 43         return value is not False."""
44         screen = None
45         stacktrace = None
46
47         end_time = time.time() + self._timeout
48         while True:
49             try:
50                 value = method(self._driver)
51                 if value:
52                     return value
53             except self._ignored_exceptions as exc:
54                 screen = getattr(exc, ‘screen‘, None)
55                 stacktrace = getattr(exc, ‘stacktrace‘, None)
56             time.sleep(self._poll)
57             if time.time() > end_time:
58                 break
59         raise TimeoutException(message, screen, stacktrace)
60
61     def until_not(self, method, message=‘‘):
62         """Calls the method provided with the driver as an argument until the 63         return value is False."""
64         end_time = time.time() + self._timeout
65         while True:
66             try:
67                 value = method(self._driver)
68                 if not value:
69                     return value
70             except self._ignored_exceptions:
71                 return True
72             time.sleep(self._poll)
73             if time.time() > end_time:
74                 break
75         raise TimeoutException(message)

 哈哈,我始终相信贴出来总会有人看。WebDriverWait 类位于selenium.webdriver.support.ui下面的例子很简单,

Example:
from selenium.webdriver.support.ui import WebDriverWait \n
element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
                 until_not(lambda x: x.find_element_by_id("someId").is_displayed())

WebDriverWait 里面主要有两个方法,一个是until和until_not

那么我们可以这样用:
WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("某个按钮")).click()

意思就是在10秒内等待某个按钮被定位到,咱们再去点击。就是每隔0.5秒内调用一下until里面的表达式或者方法函数,要么10秒内表达式执行成功,要么10秒后抛出超时异常。

然后我们再看expected_conditions:
  1 from selenium.common.exceptions import NoSuchElementException
  2 from selenium.common.exceptions import NoSuchFrameException
  3 from selenium.common.exceptions import StaleElementReferenceException
  4 from selenium.common.exceptions import WebDriverException
  5 from selenium.common.exceptions import NoAlertPresentException
  6
  7 """
  8  * Canned "Expected Conditions" which are generally useful within webdriver
  9  * tests.
 10 """
 11 class title_is(object):
 12     """An expectation for checking the title of a page.
 13     title is the expected title, which must be an exact match
 14     returns True if the title matches, false otherwise."""
 15     def __init__(self, title):
 16         self.title = title
 17
 18     def __call__(self, driver):
 19         return self.title == driver.title
 20
 21 class title_contains(object):
 22     """ An expectation for checking that the title contains a case-sensitive
 23     substring. title is the fragment of title expected
 24     returns True when the title matches, False otherwise
 25     """
 26     def __init__(self, title):
 27         self.title = title
 28
 29     def __call__(self, driver):
 30         return self.title in driver.title
 31
 32 class presence_of_element_located(object):
 33     """ An expectation for checking that an element is present on the DOM
 34     of a page. This does not necessarily mean that the element is visible.
 35     locator - used to find the element
 36     returns the WebElement once it is located
 37     """
 38     def __init__(self, locator):
 39         self.locator = locator
 40
 41     def __call__(self, driver):
 42         return _find_element(driver, self.locator)
 43
 44 class visibility_of_element_located(object):
 45     """ An expectation for checking that an element is present on the DOM of a
 46     page and visible. Visibility means that the element is not only displayed
 47     but also has a height and width that is greater than 0.
 48     locator - used to find the element
 49     returns the WebElement once it is located and visible
 50     """
 51     def __init__(self, locator):
 52         self.locator = locator
 53
 54     def __call__(self, driver):
 55         try:
 56             return _element_if_visible(_find_element(driver, self.locator))
 57         except StaleElementReferenceException:
 58             return False
 59
 60 class visibility_of(object):
 61     """ An expectation for checking that an element, known to be present on the
 62     DOM of a page, is visible. Visibility means that the element is not only
 63     displayed but also has a height and width that is greater than 0.
 64     element is the WebElement
 65     returns the (same) WebElement once it is visible
 66     """
 67     def __init__(self, element):
 68         self.element = element
 69
 70     def __call__(self, ignored):
 71         return _element_if_visible(self.element)
 72
 73 def _element_if_visible(element, visibility=True):
 74     return element if element.is_displayed() == visibility else False
 75
 76 class presence_of_all_elements_located(object):
 77     """ An expectation for checking that there is at least one element present
 78     on a web page.
 79     locator is used to find the element
 80     returns the list of WebElements once they are located
 81     """
 82     def __init__(self, locator):
 83         self.locator = locator
 84
 85     def __call__(self, driver):
 86         return _find_elements(driver, self.locator)
 87
 88 class text_to_be_present_in_element(object):
 89     """ An expectation for checking if the given text is present in the
 90     specified element.
 91     locator, text
 92     """
 93     def __init__(self, locator, text_):
 94         self.locator = locator
 95         self.text = text_
 96
 97     def __call__(self, driver):
 98         try :
 99             element_text = _find_element(driver, self.locator).text
100             return self.text in element_text
101         except StaleElementReferenceException:
102             return False
103
104 class text_to_be_present_in_element_value(object):
105     """
106     An expectation for checking if the given text is present in the element‘s
107     locator, text
108     """
109     def __init__(self, locator, text_):
110         self.locator = locator
111         self.text = text_
112
113     def __call__(self, driver):
114         try:
115             element_text = _find_element(driver,
116                                          self.locator).get_attribute("value")
117             if element_text:
118                 return self.text in element_text
119             else:
120                 return False
121         except StaleElementReferenceException:
122                 return False
123
124 class frame_to_be_available_and_switch_to_it(object):
125     """ An expectation for checking whether the given frame is available to
126     switch to.  If the frame is available it switches the given driver to the
127     specified frame.
128     """
129     def __init__(self, locator):
130         self.frame_locator = locator
131
132     def __call__(self, driver):
133         try:
134             if isinstance(self.frame_locator, tuple):
135                 driver.switch_to.frame(_find_element(driver,
136                                                      self.frame_locator))
137             else:
138                 driver.switch_to.frame(self.frame_locator)
139             return True
140         except NoSuchFrameException:
141             return False
142
143 class invisibility_of_element_located(object):
144     """ An Expectation for checking that an element is either invisible or not
145     present on the DOM.
146
147     locator used to find the element
148     """
149     def __init__(self, locator):
150         self.locator = locator
151
152     def __call__(self, driver):
153         try:
154             return _element_if_visible(_find_element(driver, self.locator), False)
155         except (NoSuchElementException, StaleElementReferenceException):
156             # In the case of NoSuchElement, returns true because the element is
157             # not present in DOM. The try block checks if the element is present
158             # but is invisible.
159             # In the case of StaleElementReference, returns true because stale
160             # element reference implies that element is no longer visible.
161             return True
162
163 class element_to_be_clickable(object):
164     """ An Expectation for checking an element is visible and enabled such that
165     you can click it."""
166     def __init__(self, locator):
167         self.locator = locator
168
169     def __call__(self, driver):
170         element = visibility_of_element_located(self.locator)(driver)
171         if element and element.is_enabled():
172             return element
173         else:
174             return False
175
176 class staleness_of(object):
177     """ Wait until an element is no longer attached to the DOM.
178     element is the element to wait for.
179     returns False if the element is still attached to the DOM, true otherwise.
180     """
181     def __init__(self, element):
182         self.element = element
183
184     def __call__(self, ignored):
185         try:
186             # Calling any method forces a staleness check
187             self.element.is_enabled()
188             return False
189         except StaleElementReferenceException as expected:
190             return True
191
192 class element_to_be_selected(object):
193     """ An expectation for checking the selection is selected.
194     element is WebElement object
195     """
196     def __init__(self, element):
197         self.element = element
198
199     def __call__(self, ignored):
200         return self.element.is_selected()
201
202 class element_located_to_be_selected(object):
203     """An expectation for the element to be located is selected.
204     locator is a tuple of (by, path)"""
205     def __init__(self, locator):
206         self.locator = locator
207
208     def __call__(self, driver):
209         return _find_element(driver, self.locator).is_selected()
210
211 class element_selection_state_to_be(object):
212     """ An expectation for checking if the given element is selected.
213     element is WebElement object
214     is_selected is a Boolean."
215     """
216     def __init__(self, element, is_selected):
217         self.element = element
218         self.is_selected = is_selected
219
220     def __call__(self, ignored):
221         return self.element.is_selected() == self.is_selected
222
223 class element_located_selection_state_to_be(object):
224     """ An expectation to locate an element and check if the selection state
225     specified is in that state.
226     locator is a tuple of (by, path)
227     is_selected is a boolean
228     """
229     def __init__(self, locator, is_selected):
230         self.locator = locator
231         self.is_selected = is_selected
232
233     def __call__(self, driver):
234         try:
235             element = _find_element(driver, self.locator)
236             return element.is_selected() == self.is_selected
237         except StaleElementReferenceException:
238             return False
239
240 class alert_is_present(object):
241     """ Expect an alert to be present."""
242     def __init__(self):
243         pass
244
245     def __call__(self, driver):
246         try:
247             alert = driver.switch_to.alert
248             alert.text
249             return alert
250         except NoAlertPresentException:
251             return False
252
253 def _find_element(driver, by):
254     """Looks up an element. Logs and re-raises ``WebDriverException``
255     if thrown."""
256     try :
257         return driver.find_element(*by)
258     except NoSuchElementException as e:
259         raise e
260     except WebDriverException as e:
261         raise e
262
263 def _find_elements(driver, by):
264     try :
265         return driver.find_elements(*by)
266     except WebDriverException as e:
267         raise e

哈哈,我相信这个py文件大家一看就能懂。这无非就是一些预期条件。

结合上面的WebDriverWait,我们可以这么用:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

‘‘‘
10秒钟等待浏览器弹出的对话框,如果出现,就点击确定按钮
‘‘‘
WebDriverWait(chromedriver,10).until(EC.alert_is_present()).accept()
我们的自动化脚本跑得快慢或者出现异常,很大程度上取决于我们设定的等待时间,如果你还是习惯于time.sleep(5)这种方式的话,这将会浪费很多时间。根据expected_conditions.py文件的写法,我们也可以定义一些自己的期待类,例如:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class must_get_url(object):
    ‘‘‘
    必须到达的URL
       参数:
       url    - 必须到达的地址
    ‘‘‘
    def __init__(self, url):
        self.url = url

    def __call__(self, driver):
        driver.get(self.url)
        return self.url == driver.current_url

options = webdriver.ChromeOptions()
options.add_argument("--test-type")
chromedriver =     webdriver.Chrome(r"E:\MyProject\WebDriver\chromedriver.exe",chrome_options=options)
WebDriverWait(chromedriver,10).until(must_get_url("https://www.baidu.com"))
				
时间: 2024-10-12 20:03:40

Selenium 高阶应用之WebDriverWait 和 expected_conditions的相关文章

python selenium 中的显示等待WebDriverWait与条件判断expected_conditions

#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait base_url = "http://www.baidu.com"

selenium (四) WebDriverWait 与 expected_conditions

在介绍WebDriverWait之前,先说一下,在selenium中的两种等待页面加载的方式,第一种是隐式等待,在webdriver里面提供的implicitly_wait()方法,driver.implicitly_wait(30) #单位:秒第二种是显示等待,是在support/wait中的WebDriverWait类中实现,可以根据需要设置等待时间和每次等待的步长.当前还有种等待方式,是Python自带time模块中的sleep()方法 这里就注重介绍 WebDriverWait 与 ex

python入门16 递归函数 高阶函数

递归函数:函数内部调用自身.(要注意跳出条件,否则会死循环) 高阶函数:函数的参数包含函数 递归函数 #coding:utf-8 #/usr/bin/python """ 2018-11-17 dinghanhua 递归函数 高阶函数 """ '''递归函数,函数内部调用函数本身''' '''n!''' def f_mul(n): if type(n) != type(1) or n <= 0: #不是整数或小于0 raise Except

js高阶函数

map()方法定义在JavaScript的Array中,我们调用Array的map()方法,传入我们自己的函数,就得到了一个新的Array作为结果: function pow(x) { return x * x; } var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; arr.map(pow); // [1, 4, 9, 16, 25, 36, 49, 64, 81] reduce()把一个函数作用在这个Array的[x1, x2, x3...]上,这个函数必须接收两个

python 高阶函数

高阶函数也遵循函数即变量的形式 高阶函数的形式: 1.把一个函数名当做实参传给另外一个函数(在不修改被装饰函数源代码的情况下为其添加功能) 如: def abc(): print('this is abc') def def(func): print(func) def(abc()) ====结果=======出来的是一个内存地址 <function agc at 0x00......> 高阶函数实例 import time def bar(): time.sleep(3) def test1

python 中的高阶函数

函数名其实就是指向函数的变量 >>> abs(-1) 1 >>> abs <built-in function abs> >>> a=abs >>> a(-1) 1 高阶函数:能接收函数做变量的函数 >>> def abc(x,y,f): ... return f(x)+f(y) ... >>> abc(-2,3,abs) 5 python中的内置高阶函数 map()函数和reduce(

Python高阶函数-闭包

高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 在这里我们首先回忆一下python代码运行的时候遇到函数是怎么做的. 从python解释器开始执行之后,就在内存中开辟了一个空间 每当遇到一个变量的时候,就把变量名和值之间的对应关系记录下来. 但是当遇到函数定义的时候解释器只是象征性的将函数名读入内存,表示知道这个函数的存在了,至于函数内部的变量和逻辑解释器根本不关心. 等执行到函数调用的时候,python解释器会再开辟一块内存来存储这个函数里的内容,这个时候,才关注函数里面有哪

Python学习笔记八:文件操作(续),文件编码与解码,函数,递归,函数式编程介绍,高阶函数

文件操作(续) 获得文件句柄位置,f.tell(),从0开始,按字符数计数 f.read(5),读取5个字符 返回文件句柄到某位置,f.seek(0) 文件在编辑过程中改变编码,f.detech() 获取文件编码,f.encoding() 获取文件在内存中的编号,f.fileno() 获取文件终端类型(tty.打印机等),f.isatty() 获取文件名,f.name() 判断文件句柄是否可移动(tty等不可移动),f.seekable() 判断文件是否可读,f.readable() 判断文件是

python高阶函数

什么是高阶函数?根据例子一步步来 变量可以指向函数 以python内置的求绝对值的函数abs为例,我们可以有下面几种调用方法 >>> abs(-10) 10 但是如果只写abs呢? >>> abs <built-in function abs> abs(-10)是函数的调用,abs是函数本身 我们知道结果可以赋值给变量,函数是否可以呢? >>> x=abs(-10) >>> x 10 y=abs >>>