selenium WebDriverWait 重写子类,赋值真实异常message

selenium.webdriver.support.wait.WebDriverWait
"""
参数
ignored_exceptions=None
"""

    def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value does not evaluate to ``False``.

        :param method: callable(WebDriver)
        :param message: optional message for :exc:`TimeoutException`
        :returns: the result of the last call to `method`
        :raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
        """
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except InvalidSelectorException as e:
                raise e
            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)

 

ignored_exceptions:默认值是 NoSuchElementException,当存在此异常时,会忽略,直到达到设置的 timeout超时时间
这里有一个弊端,当达到超时时间后,会抛出 TimeoutException,并且message只能打印传入值,不会打印实际 NoSuchElementException的值,没有参考价值
运行日志:
Traceback (most recent call last):
  File "D:\web_keys_datadriver\web_keys\keys.py", line 106, in locate
    return self.web_el_wait(name, value)
  File "D:\web_keys_datadriver\web_keys\keys.py", line 82, in web_el_wait
    EC.visibility_of_element_located((name, value)), "web_el_wait方法定位元素异常:")
  File "D:\web_keys_datadriver\web_keys\web_wait.py", line 42, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: web_el_wait方法定位元素异常:
Stacktrace:

 



可以重写WebDriverWait,代码如下
class WebDriverWaitExtend(WebDriverWait):
    def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
        super(WebDriverWaitExtend, self).__init__(driver, timeout, poll_frequency, ignored_exceptions)

    def until(self, method, message=None):
        """Calls the method provided with the driver as an argument until the \
        return value does not evaluate to ``False``.

        :param method: callable(WebDriver)
        :param message: optional message for :exc:`TimeoutException`
        :returns: the result of the last call to `method`
        :raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
        """
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except InvalidSelectorException as e:
                raise e
            except self._ignored_exceptions as exc:
                # screen = getattr(exc, 'screen', None)
                # stacktrace = getattr(exc, 'stacktrace', None)
                # 新增错误异常信息
                message += getattr(exc, 'msg', None)
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        raise TimeoutException(message, screen, stacktrace)
运行日志:
Traceback (most recent call last):
  File "D:\web_keys_datadriver\web_keys\keys.py", line 106, in locate
    return self.web_el_wait(name, value)
  File "D:\web_keys_datadriver\web_keys\keys.py", line 82, in web_el_wait
    EC.visibility_of_element_located((name, value)), "web_el_wait方法定位元素异常:")
  File "D:\web_keys_datadriver\web_keys\web_wait.py", line 42, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: web_el_wait方法定位元素异常:Unable to locate element{"method""css selector","selector""[name="accounts1"]"}(Session infochrome=71.0.3578.
98)

 




posted @ 2021-11-05 10:37  martinsun  阅读(182)  评论(0编辑  收藏  举报