最近在使用selenium2Library时,用到其中的 Wait Until Page函数,因我们的网页相对比较敏感,经常获取不到,不明觉历

看看源码吧,如下:

def wait_until_page_contains_element(self, locator, timeout=None, error=None):
"""Waits until element specified with `locator` appears on current page. Fails if `timeout` expires before the element appears. See
`introduction` for more information about `timeout` and its
default value. `error` can be used to override the default error message. See also `Wait Until Page Contains`, `Wait For Condition`,
`Wait Until Element Is Visible` and BuiltIn keyword `Wait Until
Keyword Succeeds`.
"""
if not error:
error = "Element '%s' did not appear in <TIMEOUT>" % locator
self._wait_until(timeout, error, self._is_element_present, locator)

核心的等待函数在这里

def _wait_until_no_error(self, timeout, wait_func, *args):
timeout = robot.utils.timestr_to_secs(timeout) if timeout is not None else self._timeout_in_secs
maxtime = time.time() + timeout
while True:
timeout_error = wait_func(*args)
if not timeout_error: return
if time.time() > maxtime:
raise AssertionError(timeout_error)
time.sleep(0.2)

。。。。

函数中,写死等待0.2秒,还没有参数可以改。太暴力。

再来看看selenium原生的webdriverWait是怎么写的

    def until(self, method, message=''):
"""Calls the method provided with the driver as an argument until the \
return value is not False."""
screen = None
stacktrace = None end_time = time.time() + self._timeout
while True:
try:
value = method(self._driver)
if value:
return value
except self._ignored_exceptions as exc:
screen = getattr(exc, 'screen', None)
stacktrace = getattr(exc, 'stacktrace', None)
time.sleep(self._poll)
if time.time() > end_time:
break
raise TimeoutException(message, screen, stacktrace)

从源码中可以看出,等待时间可以自定义,except除了元素不可见,也还可以加自定义的错误。比selenium2library更好用

最新文章

  1. Android Studio利用Gradle删除没有使用到的资源和代码文件
  2. JDK报错Unsupported major.minor version 52.0
  3. [Js]面向对象的拖拽
  4. Saltstack系列2:Saltstack远程执行命令
  5. JQuery中html()方法的注意事项
  6. 【原创】poj ----- 2524 Ubiquitous Religions 解题报告
  7. 基于Linux平台病毒BlackHole病毒的决心
  8. ubuntu google chrome 忽略证书错误 -- 解决自签名证书不支持的问题
  9. iOS--LaunchImage启动页设置及问题解决
  10. MyBatis基础:MyBatis入门(1)
  11. cut命令详解
  12. LeetCode 748 Shortest Completing Word 解题报告
  13. JAVA将单词首字母大写方法
  14. C#窗体操作的小技巧
  15. CentOS 编译 GCC 7.2
  16. 菜鸟译文(一)——Java中的继承和组合
  17. springboot+shiro+redis(单机redis版)整合教程
  18. 《Head First 设计模式》读书笔记
  19. 基于.NET Core2的图片上传
  20. centos7中安装mongodb3.6

热门文章

  1. Salesforce Lightning开发学习(四)重写新建/更新按钮
  2. vue系列--vue是如何实现绑定事件
  3. Hibernate 连接MySQL/SQLServer/Oracle数据库的hibernate.cfg.xml文件
  4. 多项式 ln
  5. Laravel自定义排序
  6. IntelliJ IDEA 超实用使用技巧分享
  7. mysql插入数据频繁出现坏表
  8. POJ 2249 暴力求组合数
  9. 基础知识---委托和 lambda
  10. 『快乐链覆盖 树形dp』