From 8d89e4f7ef91986aa2eb70520362a7afeaa943df Mon Sep 17 00:00:00 2001 From: g1879 Date: Fri, 25 Aug 2023 18:32:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=5Ftarget=5Fid=E5=8F=96=E4=BB=A3tab=5F?= =?UTF-8?q?id=EF=BC=8Cframe=E5=AF=B9=E8=B1=A1=E8=83=BD=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E8=BF=94=E5=9B=9Etab=20id=EF=BC=9B=E4=BF=AE=E5=A4=8Dtab?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E7=BB=A7=E6=89=BFpage=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9Bpage?= =?UTF-8?q?=E5=92=8Ctab=E5=AF=B9=E8=B1=A1=E6=B7=BB=E5=8A=A0set.download=5F?= =?UTF-8?q?path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/base.py | 7 ++++--- DrissionPage/chromium_base.py | 15 ++++++++----- DrissionPage/chromium_base.pyi | 3 +++ DrissionPage/chromium_element.py | 2 +- DrissionPage/chromium_frame.py | 12 ++++++++++- DrissionPage/chromium_frame.pyi | 3 +++ DrissionPage/chromium_page.py | 8 +++---- DrissionPage/chromium_tab.py | 9 ++++++++ DrissionPage/chromium_tab.pyi | 4 ++++ DrissionPage/setter.py | 36 ++++++++++++++++++++++++++++---- DrissionPage/setter.pyi | 17 +++++++++++---- 11 files changed, 94 insertions(+), 22 deletions(-) diff --git a/DrissionPage/base.py b/DrissionPage/base.py index 7923bb2..33aa5c1 100644 --- a/DrissionPage/base.py +++ b/DrissionPage/base.py @@ -363,10 +363,11 @@ class BasePage(BaseParser): """初始化函数""" self._url = None self.timeout = timeout if timeout is not None else 10 - self.retry_times = 3 - self.retry_interval = 2 self._url_available = None - self._download_path = '' + if not hasattr(self, 'retry_times'): + self.retry_times = 3 + if not hasattr(self, 'retry_interval'): + self.retry_interval = 2 self._DownloadKit = None @property diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index e8bd861..ffbfb8b 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -189,7 +189,7 @@ class ChromiumBase(BasePage): def _onFrameStartedLoading(self, **kwargs): """页面开始加载时触发""" - if kwargs['frameId'] == self.tab_id: + if kwargs['frameId'] == self._target_id: self._is_loading = True if self._debug: @@ -199,7 +199,7 @@ class ChromiumBase(BasePage): def _onFrameStoppedLoading(self, **kwargs): """页面加载完成后触发""" - if kwargs['frameId'] == self.tab_id and self._first_run is False and self._is_loading: + if kwargs['frameId'] == self._target_id and self._first_run is False and self._is_loading: if self._debug: print('页面停止加载 FrameStoppedLoading') if self._debug_recorder: @@ -225,7 +225,7 @@ class ChromiumBase(BasePage): def _onFrameNavigated(self, **kwargs): """页面跳转时触发""" - if kwargs['frame'].get('parentId', None) == self.tab_id and self._first_run is False and self._is_loading: + if kwargs['frame'].get('parentId', None) == self._target_id and self._first_run is False and self._is_loading: self._is_loading = True if self._debug: print('navigated') @@ -275,12 +275,12 @@ class ChromiumBase(BasePage): @property def title(self): """返回当前页面title""" - return self.run_cdp_loaded('Target.getTargetInfo', targetId=self.tab_id)['targetInfo']['title'] + return self.run_cdp_loaded('Target.getTargetInfo', targetId=self._target_id)['targetInfo']['title'] @property def url(self): """返回当前页面url""" - return self.run_cdp_loaded('Target.getTargetInfo', targetId=self.tab_id)['targetInfo']['url'] + return self.run_cdp_loaded('Target.getTargetInfo', targetId=self._target_id)['targetInfo']['url'] @property def _browser_url(self): @@ -303,6 +303,11 @@ class ChromiumBase(BasePage): @property def tab_id(self): + """返回当前标签页id""" + return self._target_id + + @property + def _target_id(self): """返回当前标签页id""" return self.driver.id if self.driver.status == 'started' else '' diff --git a/DrissionPage/chromium_base.pyi b/DrissionPage/chromium_base.pyi index ebbbd1b..810ab1a 100644 --- a/DrissionPage/chromium_base.pyi +++ b/DrissionPage/chromium_base.pyi @@ -97,6 +97,9 @@ class ChromiumBase(BasePage): @property def json(self) -> Union[dict, None]: ... + @property + def _target_id(self) -> str: ... + @property def tab_id(self) -> str: ... diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index 3750000..afcb6d4 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -463,7 +463,7 @@ class ChromiumElement(DrissionElement): node = self.page.run_cdp('DOM.describeNode', backendNodeId=self._backend_id)['node'] frame = node.get('frameId', None) - frame = frame or self.page.tab_id + frame = frame or self.page._target_id try: result = self.page.run_cdp('Page.getResourceContent', frameId=frame, url=src) diff --git a/DrissionPage/chromium_frame.py b/DrissionPage/chromium_frame.py index 5fe0031..d9d797d 100644 --- a/DrissionPage/chromium_frame.py +++ b/DrissionPage/chromium_frame.py @@ -17,10 +17,15 @@ from .waiter import FrameWaiter class ChromiumFrame(ChromiumBase): def __init__(self, page, ele): + """ + :param page: frame所在的页面对象 + :param ele: frame所在元素 + """ self.page = page self.address = page.address node = page.run_cdp('DOM.describeNode', backendNodeId=ele.ids.backend_id)['node'] self.frame_id = node['frameId'] + self._tab_id = page.tab_id self._backend_id = ele.ids.backend_id self._frame_ele = ele self._states = None @@ -329,6 +334,11 @@ class ChromiumFrame(ChromiumBase): self._wait = FrameWaiter(self) return self._wait + @property + def tab_id(self): + """返回frame所在tab的id""" + return self._tab_id + def refresh(self): """刷新frame页面""" self._check_ok() @@ -616,7 +626,7 @@ class ChromiumFrameIds(object): @property def tab_id(self): """返回当前标签页id""" - return self._frame.page.tab_id + return self._frame._tab_id @property def backend_id(self): diff --git a/DrissionPage/chromium_frame.pyi b/DrissionPage/chromium_frame.pyi index a2bdce8..df14080 100644 --- a/DrissionPage/chromium_frame.pyi +++ b/DrissionPage/chromium_frame.pyi @@ -114,6 +114,9 @@ class ChromiumFrame(ChromiumBase): @property def wait(self) -> FrameWaiter: ... + @property + def tab_id(self) -> str: ... + def refresh(self) -> None: ... def attr(self, attr: str) -> Union[str, None]: ... diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 6158afd..e6f3dff 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -99,10 +99,6 @@ class ChromiumPage(ChromiumBase): self._rect = None self._main_tab = self.tab_id - # try: - # self.download_set.by_browser() - # except CDPError: - # pass self._process_id = None r = self.browser_driver.call_method('SystemInfo.getProcessInfo') @@ -440,6 +436,10 @@ class ChromiumTabRect(object): return self._page.browser_driver.call_method('Browser.getWindowForTarget', targetId=self._page.tab_id)['bounds'] +class BrowserDownloadManager(object): + def __init__(self, page): + self._page = page + self.frames = {} # class BaseDownloadSetter(DownloadSetter): # """用于设置下载参数的类""" # diff --git a/DrissionPage/chromium_tab.py b/DrissionPage/chromium_tab.py index 9279600..6fdfa38 100644 --- a/DrissionPage/chromium_tab.py +++ b/DrissionPage/chromium_tab.py @@ -8,6 +8,7 @@ from copy import copy from .chromium_base import ChromiumBase from .commons.web import set_session_cookies, set_browser_cookies from .session_page import SessionPage +from .setter import TabSetter from .setter import WebPageTabSetter @@ -28,6 +29,7 @@ class ChromiumTab(ChromiumBase): self.retry_times = self.page.retry_times self.retry_interval = self.page.retry_interval self._page_load_strategy = self.page.page_load_strategy + self._download_path = self.page.download_path def close(self): """关闭当前标签页""" @@ -38,6 +40,13 @@ class ChromiumTab(ChromiumBase): """返回获取窗口坐标和大小的对象""" return self.page.rect + @property + def set(self): + """返回用于等待的对象""" + if self._set is None: + self._set = TabSetter(self) + return self._set + class WebPageTab(SessionPage, ChromiumTab): def __init__(self, page, tab_id): diff --git a/DrissionPage/chromium_tab.pyi b/DrissionPage/chromium_tab.pyi index 04f3ad6..fecc3fb 100644 --- a/DrissionPage/chromium_tab.pyi +++ b/DrissionPage/chromium_tab.pyi @@ -13,6 +13,7 @@ from .chromium_frame import ChromiumFrame from .chromium_page import ChromiumPage, ChromiumTabRect from .session_element import SessionElement from .session_page import SessionPage +from .setter import TabSetter from .setter import WebPageTabSetter from .web_page import WebPage @@ -29,6 +30,9 @@ class ChromiumTab(ChromiumBase): @property def rect(self) -> ChromiumTabRect: ... + @property + def set(self) -> TabSetter: ... + class WebPageTab(SessionPage, ChromiumTab): def __init__(self, page: WebPage, tab_id: str): diff --git a/DrissionPage/setter.py b/DrissionPage/setter.py index 8cce32c..8343cc2 100644 --- a/DrissionPage/setter.py +++ b/DrissionPage/setter.py @@ -118,7 +118,23 @@ class ChromiumBaseSetter(object): self._page.run_cdp('Network.setExtraHTTPHeaders', headers=headers) -class ChromiumPageSetter(ChromiumBaseSetter): +class DownloadSetter(object): + def download_path(self, path): + """设置下载路径 + :param path: 下载路径 + :return: None + """ + self._page._download_path = str(path) + if self._page._DownloadKit: + self._page._DownloadKit.set.goal_path(path) + + +class TabSetter(ChromiumBaseSetter, DownloadSetter): + def __init__(self, page): + super().__init__(page) + + +class ChromiumPageSetter(ChromiumBaseSetter, DownloadSetter): def main_tab(self, tab_id=None): """设置主tab :param tab_id: 标签页id,不传入则设置当前tab @@ -143,8 +159,11 @@ class ChromiumPageSetter(ChromiumBaseSetter): self._page._control_session.get(f'http://{self._page.address}/json/activate/{tab_or_id}') -class SessionPageSetter(object): +class SessionPageSetter(DownloadSetter): def __init__(self, page): + """ + :param page: SessionPage对象 + """ self._page = page def retry_times(self, times): @@ -155,6 +174,15 @@ class SessionPageSetter(object): """设置连接失败时重连间隔""" self._page.retry_interval = interval + def download_path(self, path): + """设置下载路径 + :param path: 下载路径 + :return: None + """ + self._page._download_path = str(path) + if self._page._DownloadKit: + self._page._DownloadKit.set.goal_path(path) + def timeout(self, second): """设置连接超时时间 :param second: 秒数 @@ -274,7 +302,7 @@ class SessionPageSetter(object): self._page.session.mount(url, adapter) -class WebPageSetter(ChromiumPageSetter): +class WebPageSetter(ChromiumPageSetter, DownloadSetter): def __init__(self, page): super().__init__(page) self._session_setter = SessionPageSetter(self._page) @@ -308,7 +336,7 @@ class WebPageSetter(ChromiumPageSetter): self._chromium_setter.user_agent(ua, platform) -class WebPageTabSetter(ChromiumBaseSetter): +class WebPageTabSetter(ChromiumBaseSetter, DownloadSetter): def __init__(self, page): super().__init__(page) self._session_setter = SessionPageSetter(self._page) diff --git a/DrissionPage/setter.pyi b/DrissionPage/setter.pyi index e750130..dfddd59 100644 --- a/DrissionPage/setter.pyi +++ b/DrissionPage/setter.pyi @@ -4,6 +4,7 @@ @Contact : g1879@qq.com """ from http.cookiejar import Cookie +from pathlib import Path from typing import Union, Tuple from requests.adapters import HTTPAdapter @@ -50,7 +51,15 @@ class ChromiumBaseSetter(object): def upload_files(self, files: Union[str, list, tuple]) -> None: ... -class ChromiumPageSetter(ChromiumBaseSetter): +class DownloadSetter(object): + def download_path(self, path: Union[str, Path]) -> None: ... + + +class TabSetter(ChromiumBaseSetter, DownloadSetter): + def __init__(self, page): ... + + +class ChromiumPageSetter(ChromiumBaseSetter, DownloadSetter): _page: ChromiumPage = ... def main_tab(self, tab_id: str = None) -> None: ... @@ -61,7 +70,7 @@ class ChromiumPageSetter(ChromiumBaseSetter): def tab_to_front(self, tab_or_id: Union[str, ChromiumTab] = None) -> None: ... -class SessionPageSetter(object): +class SessionPageSetter(DownloadSetter): def __init__(self, page: SessionPage): self._page: SessionPage = ... @@ -102,7 +111,7 @@ class SessionPageSetter(object): def add_adapter(self, url: str, adapter: HTTPAdapter) -> None: ... -class WebPageSetter(ChromiumPageSetter): +class WebPageSetter(ChromiumPageSetter, DownloadSetter): _page: WebPage = ... _session_setter: SessionPageSetter = ... _chromium_setter: ChromiumPageSetter = ... @@ -114,7 +123,7 @@ class WebPageSetter(ChromiumPageSetter): def cookies(self, cookies) -> None: ... -class WebPageTabSetter(ChromiumBaseSetter): +class WebPageTabSetter(ChromiumBaseSetter, DownloadSetter): _page: WebPage = ... _session_setter: SessionPageSetter = ... _chromium_setter: ChromiumBaseSetter = ...