zoukankan      html  css  js  c++  java
  • robot framework Selenium2library wait小问题

    最近在使用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更好用

  • 相关阅读:
    如何查看操作系统的具体版本号
    Review of Image Super-resolution Reconstruction Based on Deep Learning
    opencv imshow图片显示不全
    Javaweb文件下载异常
    Microsoft Edge出现错误代码:STATUS_INVALID_IMAGE_HASH
    Javaweb导入excel数据
    Java读取execl数据
    浏览器网页左上角小图标实现方式
    Java LDAP验证
    Java JPA新增数据生成UUID
  • 原文地址:https://www.cnblogs.com/landhu/p/11364347.html
Copyright © 2011-2022 走看看