selenium之WebDriverWait



之前有篇文章介绍过Selenium的等待知识,可惜当时水平有限,只介绍了sleep和implicitly_wait的用法。今天还是补充一下WebDriverWait的知识吧。

一、webdrivewait 示例代码


  
  
  1. from selenium import webdriver
  2. from selenium.webdriver.support.ui import WebDriverWait
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium.webdriver.common.by import By
  5. import time
  6. driver = webdriver.Chrome()
  7. driver.get( "http://www.baidu.com/")
  8. locator = (By.ID, "kw")
  9. try:
  10. ele = WebDriverWait(driver, 10).until(EC.presence_of_element_located(locator))
  11. driver.find_element_by_id( "kw").send_keys( 'abc')
  12. time.sleep( 1) #为了看效果
  13. except:
  14. print( "ele can't find")
  15. finally:
  16. driver.quit()
接下来一点点分析下这个语句
WebDriverWait(driver,10).until(EC.presence_of_element_located(locator))
  
  

(1)WebDriverWait 方法


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

(2)WebDriverWait 方法后面有两种等待方式


until


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


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

看了以上内容基本上很清楚了,调用方法如下:

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

这里需要特别注意的是until或until_not中的可执行方法method参数,很多人传入了WebElement对象,如下:

WebDriverWait(driver, 10).until(driver.find_element_by_id('kw'))  # 错误
  
  
  • 1

这是错误的用法,这里的参数一定要是可以调用的,即这个对象一定有 __call__() 方法,否则会抛出异常:

TypeError: 'xxx' object is not callable
  
  
  • 1

在这里,你可以用selenium提供的 expected_conditions 模块中的各种条件,也可以用WebElement的 is_displayed() 、is_enabled()is_selected() 方法,或者用自己封装的方法都可以,那么接下来我们看一下selenium提供的条件有哪些:

expected_conditions



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

selenium.webdriver.support.expected_conditions(模块)

  
  
  • 1
  • 2

以下两个条件类验证title,验证传入的参数title是否等于或包含于driver.title 
title_is 
title_contains

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

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

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

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

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

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

以下四个条件判断元素是否被选中,第一个条件传入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

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

上面是所有17个condition,与until、until_not组合能够实现很多判断,如果能自己灵活封装,将会大大提高脚本的稳定性。

封装示例:

待补充


定义目录标题)

猜你喜欢

转载自blog.csdn.net/qq_42293590/article/details/82988237
今日推荐