From fbeb9056c4127c5d475caa76750d6f9382d98bb0 Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 30 Mar 2023 18:15:23 +0800 Subject: [PATCH] =?UTF-8?q?3.2.22set=5Ftargets()=E5=A2=9E=E5=8A=A0is=5Freg?= =?UTF-8?q?ex=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_base.py | 23 ++++++++++++++--------- DrissionPage/chromium_base.pyi | 3 ++- DrissionPage/commons/browser.py | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index c1b5192..04b44d3 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -1031,6 +1031,7 @@ class ChromiumBaseWaiter(object): self._response = None self._request_id = None self._targets = None + self._is_regex = False def ele_delete(self, loc_or_ele, timeout=None): """等待元素从DOM中删除 @@ -1096,16 +1097,18 @@ class ChromiumBaseWaiter(object): sleep(gap) return False - def set_targets(self, regexes): + def set_targets(self, targets, is_regex=False): """指定要等待的数据包 - :param regexes: 要匹配的数据包url特征,正则字符串,可用list等传入多个 + :param targets: 要匹配的数据包url特征,可用list等传入多个 + :param is_regex: 设置的target是否正则表达式 :return: None """ - if not isinstance(regexes, (str, list, tuple, set)): + if not isinstance(targets, (str, list, tuple, set)): raise TypeError('targets只能是str、list、tuple、set。') - self._targets = regexes if isinstance(regexes, str) else set(regexes) + self._is_regex = is_regex + self._targets = targets if isinstance(targets, str) else set(targets) self._driver.run_cdp('Network.enable') - if regexes is not None: + if targets is not None: self._driver.driver.Network.responseReceived = self._response_received self._driver.driver.Network.loadingFinished = self._loading_finished else: @@ -1118,7 +1121,7 @@ class ChromiumBaseWaiter(object): def data_packets(self, targets=None, timeout=None, any_one=False): """等待指定数据包加载完成 - :param targets: 要匹配的数据包url特征,为正则字符串,可用list等传入多个 + :param targets: 要匹配的数据包url特征,可用list等传入多个 :param timeout: 超时时间,为None则使用页面对象timeout :param any_one: 多个target时,是否全部监听到才结束,为True时监听到一个目标就结束 :return: ResponseData对象或监听结果字典 @@ -1126,7 +1129,7 @@ class ChromiumBaseWaiter(object): if self._targets is None and targets is None: targets = '' if targets is not None: - self.set_targets(targets) + self.set_targets(targets, is_regex=self._is_regex) self._request_id = None self._response_result = None @@ -1148,7 +1151,8 @@ class ChromiumBaseWaiter(object): def _response_received(self, **kwargs): """接收到返回信息时处理方法""" if isinstance(self._targets, str): - if search(self._targets, kwargs['response']['url']): + if (self._is_regex and search(self._targets, kwargs['response']['url'])) or ( + not self._is_regex and self._targets in kwargs['response']['url']): self._request_id = kwargs['requestId'] self._response = kwargs['response'] @@ -1159,7 +1163,8 @@ class ChromiumBaseWaiter(object): self._request_id = {} for target in self._targets: - if search(target, kwargs['response']['url']): + if (self._is_regex and search(target, kwargs['response']['url'])) or ( + not self._is_regex and target in kwargs['response']['url']): self._response[target] = kwargs['response'] self._request_id[kwargs['requestId']] = target diff --git a/DrissionPage/chromium_base.pyi b/DrissionPage/chromium_base.pyi index 15f39ab..719c486 100644 --- a/DrissionPage/chromium_base.pyi +++ b/DrissionPage/chromium_base.pyi @@ -221,6 +221,7 @@ class ChromiumBaseWaiter(object): self._request_id: Union[str, dict] = ... self._response: dict = ... self._response_result: Union[ResponseData, Dict[str, ResponseData], False] = ... + self._is_regex: bool = ... def ele_delete(self, loc_or_ele: Union[str, tuple, ChromiumElement], timeout: float = None) -> bool: ... @@ -234,7 +235,7 @@ class ChromiumBaseWaiter(object): def load_complete(self, timeout: float = None) -> bool: ... - def set_targets(self, regexes: Union[str, list, tuple, set]) -> None: ... + def set_targets(self, targets: Union[str, list, tuple, set], is_regex: bool = False) -> None: ... def stop_listening(self) -> None: ... diff --git a/DrissionPage/commons/browser.py b/DrissionPage/commons/browser.py index 38e565e..555016f 100644 --- a/DrissionPage/commons/browser.py +++ b/DrissionPage/commons/browser.py @@ -172,7 +172,7 @@ def _run_browser(port, path: str, args) -> Popen: :return: 进程对象 """ p = Path(path) - p = str(p / 'chrome.exe') if p.is_dir() else str(path) + p = str(p / 'chrome') if p.is_dir() else str(path) arguments = [p, f'--remote-debugging-port={port}'] arguments.extend(args) return Popen(arguments, shell=False)