测试脚本中的等待方法

【说明】

1:等待是为了使脚本执行更加稳定
2:常用的休眠方式:time模块的sleep方法

【selenium模块中的等待方法】

表达式说明
implicitly_wait()设置webdriver等待时间
WebDriverWait等待条件满足或者超时后退出 from selenium.webdriver.support.ui import WebDriverWait

【代码】

#设置webdriver等待时间
>>> d=webdriver.Firefox()
>>> d.get('http://www.baidu.com')
>>> d.find_element_by_id('kw').send_keys('麦子学院')
>>> d.implicitly_wait(5)
>>> d.find_element_by_id('kw1')

#等待条件满足或者超时后退出
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> help(WebDriverWait)
Help on class WebDriverWait in module selenium.webdriver.support.wait:
#说明
class WebDriverWait(builtins.object)
 |  Methods defined here:
 |  
 |  __init__(self, driver, timeout, poll_frequency=0.5, 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())
 |  
 |  __repr__(self)
 |      Return repr(self).
 |  
 |  until(self, method, message='')
 |      Calls the method provided with the driver as an argument until the         return value is not False.
 |  
 |  until_not(self, method, message='')
 |      Calls the method provided with the driver as an argument until the         return value is False.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

#部分实例
def get_ele_times(driver, times, func):
    return WebDriverWait(driver, times).until(func)

 ele_login = get_ele_times(d, 10, lambda d: d.find_element_by_link_text(arg['text_id']))
 ele_login.click()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值