diff --git a/DrissionPage/network_listener.py b/DrissionPage/network_listener.py index 92f473b..ee11c08 100644 --- a/DrissionPage/network_listener.py +++ b/DrissionPage/network_listener.py @@ -174,7 +174,8 @@ class NetworkListener(object): def _loading_finished(self, **kwargs): """请求完成时处理方法""" request_id = kwargs['requestId'] - if request_id in self._request_ids: + dp = self._request_ids.get(request_id) + if dp: try: r = self._page.run_cdp('Network.getResponseBody', requestId=request_id) body = r['body'] @@ -183,7 +184,6 @@ class NetworkListener(object): body = '' is_base64 = False - dp = self._request_ids[request_id] dp._raw_body = body dp._base64_body = is_base64 diff --git a/DrissionPage/waiter.py b/DrissionPage/waiter.py index 3ea50d5..25b98a1 100644 --- a/DrissionPage/waiter.py +++ b/DrissionPage/waiter.py @@ -78,6 +78,55 @@ class ChromiumBaseWaiter(object): while self._driver._upload_list: sleep(.01) + def url_change(self, text, exclude=False, timeout=None, raise_err=None): + """等待url变成包含或不包含指定文本 + :param text: 用于识别的文本 + :param exclude: 是否排除,为True时当url不包含text指定文本时返回True + :param timeout: 超时时间 + :param raise_err: 等待失败时是否报错,为None时根据Settings设置 + :return: 是否等待成功 + """ + return self._change('url', text, exclude, timeout, raise_err) + + def title_change(self, text, exclude=False, timeout=None, raise_err=None): + """等待title变成包含或不包含指定文本 + :param text: 用于识别的文本 + :param exclude: 是否排除,为True时当title不包含text指定文本时返回True + :param timeout: 超时时间 + :param raise_err: 等待失败时是否报错,为None时根据Settings设置 + :return: 是否等待成功 + """ + return self._change('title', text, exclude, timeout, raise_err) + + def _change(self, arg, text, exclude=False, timeout=None, raise_err=None): + """等待指定属性变成包含或不包含指定文本 + :param arg: 要被匹配的属性 + :param text: 用于识别的文本 + :param exclude: 是否排除,为True时当属性不包含text指定文本时返回True + :param timeout: 超时时间 + :param raise_err: 等待失败时是否报错,为None时根据Settings设置 + :return: 是否等待成功 + """ + if timeout is None: + timeout = self._driver.timeout + + end_time = perf_counter() + timeout + while perf_counter() < end_time: + if arg == 'url': + val = self._driver.url + elif arg == 'title': + val = self._driver.title + else: + raise ValueError + if (not exclude and text in val) or (exclude and text not in val): + return True + sleep(.05) + + if raise_err is True or Settings.raise_when_wait_failed is True: + raise WaitTimeoutError(f'等待{arg}改变失败。') + else: + return False + def _loading(self, timeout=None, start=True, gap=.01, raise_err=None): """等待页面开始加载或加载完成 :param timeout: 超时时间,为None时使用页面timeout属性 diff --git a/DrissionPage/waiter.pyi b/DrissionPage/waiter.pyi index 41ea5e9..548c167 100644 --- a/DrissionPage/waiter.pyi +++ b/DrissionPage/waiter.pyi @@ -35,6 +35,13 @@ class ChromiumBaseWaiter(object): def upload_paths_inputted(self) -> None: ... + def url_change(self, text: str, exclude: bool = False, timeout: float = None, raise_err: bool = None) -> bool: ... + + def title_change(self, text: str, exclude: bool = False, timeout: float = None, raise_err: bool = None) -> bool: ... + + def _change(self, arg: str, text: str, exclude: bool = False, timeout: float = None, + raise_err: bool = None) -> bool: ... + class ChromiumPageWaiter(ChromiumBaseWaiter): _driver: ChromiumPage = ...