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