python webdriver中的等待---如何正确编写

2017年11月15日16:07:20,今天终于搞清楚了显性等待的写法,纪录以备后用。文档链接地址:http://blog.csdn.net/huilan_same/article/details/52544521 

1、需要导入的类

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

2、写法

locator = (By.ID, 'name')
WebDriverWait(driver, 20, 0.5).until(EC.element_to_be_clickable(locator))

WebDriverWait(driver, 20, 0.5).until_not(EC.element_to_be_clickable(locator))

WebDriverWait(driver, 超时时长, 调用频率, 忽略异常).until(可执行方法, 超时时返回的信息)

3、解释

WebDriverWait

selenium.webdriver.support.wait.WebDriverWait(类)

wait模块下的WebDriverWait类是显性等待类,它有如下的参数与方法:

1)__init__

driver: 传入WebDriver实例,即我们上例中的driver
timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间)
poll_frequency: 调用until或until_not中的方法的间隔时间,默认是0.5秒
ignored_exceptions: 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常,
        则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException。

2)unit

method: 在等待期间,每隔一段时间(__init__中的poll_frequency)调用这个传入的方法,直到返回值不是False
message: 如果超时,抛出TimeoutException,将message传入异常

3)unitl_not

与until相反,until是当某元素出现或什么条件成立则继续执行,
 until_not是当某元素消失或什么条件不成立则继续执行,参数也相同,不再赘述。

4)最终的写法为:WebDriverWait(driver, 超时时长, 调用频率, 忽略异常).until(expected_conditions.可执行方法, 超时时返回的信息)

注意unitl中的方法必须是可执行的,不然会报错:TypeError: 'xxx' object is not callable

 4、报错TypeError: 'xxx' object is not callable时的解决方法

要使用正确的写法

locator = (By.ID, 'name')
WebDriverWait(driver, 20, 0.5).until(EC.element_to_be_clickable(locator))

WebDriverWait(driver, 20, 0.5).until(expected_conditions.可执行方法(locator))

1)locator

  locator是指selenium中By下提供的可以查找元素的方法。

  需要导入的类是:from selenium.webdriver.common.by import By

  具体具有的方法有很多,具体在:d:\install\python27\lib\site-packages\selenium-3.6.0-py2.7.egg\selenium\webdriver\chrome\webdriver.py下查看源码。

2)expected_conditions.可执行方法


  expected_conditions是selenium的一个模块,其中包含一系列可用于判断的条件:

  selenium.webdriver.support.expected_conditions(模块)

  具体的可用的判断条件有如下:

  1、验证title,验证传入的参数title是否等于或包含于driver.title

    title_is

    title_contains

    WebDriverWait(driver, 20, 0.5).until(EC.title_is(u"翼课网,掌握学习之道"))

  2、验证元素是否出现传入的参数都是元组类型的locator,如(By.ID, ‘kw’) 
  顾名思义,一个只要一个符合条件的元素加载出来就通过;另一个必须所有符合条件的元素都加载出来才行 
    presence_of_element_located 
    presence_of_all_elements_located

    locator = (By.ID, 'name')

     WebDriverWait(driver, 20, 0.5).until(EC.element_to_be_clickable(locator))

  3、以下三个条件验证元素是否可见,前两个传入参数是元组类型的locator,第三个传入WebElement 第一个和第三个其实质是一样的 

    visibility_of_element_located 
    invisibility_of_element_located 
    visibility_of

  4、以下两个条件判断某段文本是否出现在某元素中,一个判断元素的text,一个判断元素的value 
    text_to_be_present_in_element 
    text_to_be_present_in_element_value

  5、以下条件判断frame是否可切入,可传入locator元组或者直接传入定位方式:id、name、index或WebElement 
    frame_to_be_available_and_switch_to_it

  6、以下条件判断是否有alert出现 
    alert_is_present

  7、以下条件判断元素是否可点击,传入locator 
    element_to_be_clickable

  8、以下四个条件判断元素是否被选中,第一个条件传入WebElement对象,第二个传入locator元组 
  第三个传入WebElement对象以及状态,相等返回True,否则返回False 
  第四个传入locator以及状态,相等返回True,否则返回False 
    element_to_be_selected 
    element_located_to_be_selected 
    element_selection_state_to_be 
    element_located_selection_state_to_be

  9、最后一个条件判断一个元素是否仍在DOM中,传入WebElement对象,可以判断页面是否刷新了 
    staleness_of

posted on 2017-11-15 16:45  香水百合2  阅读(46)  评论(0)    收藏  举报