diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 39b03f6..f033019 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -979,7 +979,7 @@ class ChromiumBaseWaiter(object): :return: 是否等待成功 """ ele = self._driver._ele(loc_or_ele, raise_err=False) - return ele.wait.display(timeout) + return ele.wait.display(timeout) if ele else False def ele_hidden(self, loc_or_ele, timeout=None): """等待元素变成隐藏状态 diff --git a/DrissionPage/chromium_base.pyi b/DrissionPage/chromium_base.pyi index f14f6c1..d2aba1d 100644 --- a/DrissionPage/chromium_base.pyi +++ b/DrissionPage/chromium_base.pyi @@ -7,7 +7,6 @@ from pathlib import Path from typing import Union, Tuple, List, Any, Dict from DataRecorder import Recorder -from FlowViewer.listener import ResponseData from requests import Session from requests.cookies import RequestsCookieJar @@ -232,14 +231,14 @@ class ChromiumBaseWaiter(object): def load_complete(self, timeout: float = None) -> bool: ... + def upload_paths_inputted(self) -> None: ... + def set_targets(self, targets: Union[str, list, tuple, set], is_regex: bool = False) -> None: ... def stop_listening(self) -> None: ... def data_packets(self, timeout: float = None, - any_one: bool = False) -> Union[ResponseData, Dict[str, ResponseData], False]: ... - - def upload_paths_inputted(self) -> None: ... + any_one: bool = False) -> Union[DataPacket, Dict[str, List[DataPacket]], False]: ... class NetworkListener(object): @@ -249,7 +248,7 @@ class NetworkListener(object): self._caught: int = ... self._targets: Union[str, dict] = ... self._single: bool = ... - self._results: Union[ResponseData, Dict[str, DataPacket], False] = ... + self._results: Union[DataPacket, Dict[str, List[DataPacket]], False] = ... self._is_regex: bool = ... self._requests: dict = ... @@ -258,7 +257,7 @@ class NetworkListener(object): def stop(self) -> None: ... def listen(self, timeout: float = None, - any_one: bool = False) -> Union[ResponseData, Dict[str, ResponseData], False]: ... + any_one: bool = False) -> Union[DataPacket, Dict[str, List[DataPacket]], False]: ... def _request_will_sent(self, **kwargs) -> None: ... diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index e76d26a..18a19d4 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -5,13 +5,14 @@ """ from pathlib import Path from platform import system +from re import search from threading import Thread from time import perf_counter, sleep from warnings import warn from requests import Session -from .chromium_base import ChromiumBase, Timeout, ChromiumBaseSetter, ChromiumBaseWaiter +from .chromium_base import ChromiumBase, Timeout, ChromiumBaseSetter, ChromiumBaseWaiter, NetworkListener from .chromium_driver import ChromiumDriver from .chromium_tab import ChromiumTab from .commons.browser import connect_browser @@ -379,6 +380,10 @@ class ChromiumPage(ChromiumBase): class ChromiumPageWaiter(ChromiumBaseWaiter): + def __init__(self, page: ChromiumBase): + super().__init__(page) + self._listener = None + def download_begin(self, timeout=None): """等待浏览器下载开始 :param timeout: 等待超时时间,为None则使用页面对象timeout属性 @@ -396,6 +401,32 @@ class ChromiumPageWaiter(ChromiumBaseWaiter): while self._driver.tab_id == self._driver.latest_tab and perf_counter() < end_time: sleep(.01) + def set_targets(self, targets, is_regex=False): + """指定要等待的数据包 + :param targets: 要匹配的数据包url特征,可用list等传入多个 + :param is_regex: 设置的target是否正则表达式 + :return: None + """ + if not self._listener: + self._listener = NetworkListener(self._driver) + self._listener.set_targets(targets, is_regex) + + def data_packets(self, timeout=None, any_one=False): + """等待指定数据包加载完成 + :param timeout: 超时时间,为None则使用页面对象timeout + :param any_one: 多个target时,是否全部监听到才结束,为True时监听到一个目标就结束 + :return: ResponseData对象或监听结果字典 + """ + if not self._listener: + self._listener = NetworkListener(self._driver) + return self._listener.listen(timeout, any_one) + + def stop_listening(self): + """停止监听数据包""" + if not self._listener: + self._listener = NetworkListener(self._driver) + self._listener.stop() + class ChromiumTabRect(object): def __init__(self, page): diff --git a/DrissionPage/chromium_page.pyi b/DrissionPage/chromium_page.pyi index 30756c9..f05a3bd 100644 --- a/DrissionPage/chromium_page.pyi +++ b/DrissionPage/chromium_page.pyi @@ -6,14 +6,15 @@ from os import popen from pathlib import Path from threading import Thread -from typing import Union, Tuple, List +from typing import Union, Tuple, List, Dict from DownloadKit import DownloadKit from requests import Session -from .chromium_base import ChromiumBase, ChromiumBaseSetter, ChromiumBaseWaiter +from .chromium_base import ChromiumBase, ChromiumBaseSetter, ChromiumBaseWaiter, NetworkListener from .chromium_driver import ChromiumDriver from .chromium_tab import ChromiumTab +from .commons.web import DataPacket from .configs.chromium_options import ChromiumOptions from .configs.driver_options import DriverOptions from .session_page import DownloadSetter @@ -111,11 +112,19 @@ class ChromiumPage(ChromiumBase): class ChromiumPageWaiter(ChromiumBaseWaiter): _driver: ChromiumPage = ... + _listener: Union[NetworkListener, None] = ... def download_begin(self, timeout: float = None) -> bool: ... def new_tab(self, timeout: float = None) -> bool: ... + def set_targets(self, targets: Union[str, list, tuple, set], is_regex: bool = False) -> None: ... + + def stop_listening(self) -> None: ... + + def data_packets(self, timeout: float = None, + any_one: bool = False) -> Union[DataPacket, Dict[str, List[DataPacket]], False]: ... + class ChromiumTabRect(object): def __init__(self, page: ChromiumPage): diff --git a/DrissionPage/commons/web.py b/DrissionPage/commons/web.py index e806a9f..50804b3 100644 --- a/DrissionPage/commons/web.py +++ b/DrissionPage/commons/web.py @@ -17,20 +17,20 @@ from tldextract import extract class DataPacket(object): """返回的数据包管理类""" - __slots__ = ('requestId', 'request', 'response', 'rawBody', 'tab', 'target', '_requestHeaders', '_body', - '_postData', '_request_data', '_response_data', '_fail_info', - # cdp 原始数据 - '_raw_request', '_raw_response', '_raw_fail_info', '_rawPostData', '_rawBody', '_base64_body', - - 'url', 'urlFragment', 'method', 'postDataEntries', 'mixedContentType', 'initialPriority', - 'referrerPolicy', 'isLinkPreload', 'trustTokenParams', 'isSameSite', - - 'status', 'statusText', - 'securityDetails', 'headersText', 'mimeType', 'requestHeadersText', 'connectionReused', 'connectionId', - 'remoteIPAddress', 'remotePort', 'fromDiskCache', 'fromServiceWorker', 'fromPrefetchCache', - 'encodedDataLength', 'timing', 'serviceWorkerResponseSource', 'responseTime', 'cacheStorageCacheName', - 'protocol', 'securityState', - ) + # __slots__ = ('requestId', 'request', 'response', 'rawBody', 'tab', 'target', '_requestHeaders', '_body', + # '_postData', '_request_data', '_response_data', '_fail_info', + # # cdp 原始数据 + # '_raw_request', '_raw_response', '_raw_fail_info', '_rawPostData', '_rawBody', '_base64_body', + # + # 'url', 'urlFragment', 'method', 'postDataEntries', 'mixedContentType', 'initialPriority', + # 'referrerPolicy', 'isLinkPreload', 'trustTokenParams', 'isSameSite', + # + # 'status', 'statusText', + # 'securityDetails', 'headersText', 'mimeType', 'requestHeadersText', 'connectionReused', 'connectionId', + # 'remoteIPAddress', 'remotePort', 'fromDiskCache', 'fromServiceWorker', 'fromPrefetchCache', + # 'encodedDataLength', 'timing', 'serviceWorkerResponseSource', 'responseTime', 'cacheStorageCacheName', + # 'protocol', 'securityState', + # ) def __init__(self, request_id, tab, target, raw_request): """