用_target_id取代tab_id,frame对象能正确返回tab id;修复tab没有继承page下载路径的问题;page和tab对象添加set.download_path

This commit is contained in:
g1879 2023-08-25 18:32:11 +08:00
parent fa918f105a
commit 8d89e4f7ef
11 changed files with 94 additions and 22 deletions

View File

@ -363,10 +363,11 @@ class BasePage(BaseParser):
"""初始化函数""" """初始化函数"""
self._url = None self._url = None
self.timeout = timeout if timeout is not None else 10 self.timeout = timeout if timeout is not None else 10
self.retry_times = 3
self.retry_interval = 2
self._url_available = None 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 self._DownloadKit = None
@property @property

View File

@ -189,7 +189,7 @@ class ChromiumBase(BasePage):
def _onFrameStartedLoading(self, **kwargs): def _onFrameStartedLoading(self, **kwargs):
"""页面开始加载时触发""" """页面开始加载时触发"""
if kwargs['frameId'] == self.tab_id: if kwargs['frameId'] == self._target_id:
self._is_loading = True self._is_loading = True
if self._debug: if self._debug:
@ -199,7 +199,7 @@ class ChromiumBase(BasePage):
def _onFrameStoppedLoading(self, **kwargs): 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: if self._debug:
print('页面停止加载 FrameStoppedLoading') print('页面停止加载 FrameStoppedLoading')
if self._debug_recorder: if self._debug_recorder:
@ -225,7 +225,7 @@ class ChromiumBase(BasePage):
def _onFrameNavigated(self, **kwargs): 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 self._is_loading = True
if self._debug: if self._debug:
print('navigated') print('navigated')
@ -275,12 +275,12 @@ class ChromiumBase(BasePage):
@property @property
def title(self): def title(self):
"""返回当前页面title""" """返回当前页面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 @property
def url(self): def url(self):
"""返回当前页面url""" """返回当前页面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 @property
def _browser_url(self): def _browser_url(self):
@ -303,6 +303,11 @@ class ChromiumBase(BasePage):
@property @property
def tab_id(self): def tab_id(self):
"""返回当前标签页id"""
return self._target_id
@property
def _target_id(self):
"""返回当前标签页id""" """返回当前标签页id"""
return self.driver.id if self.driver.status == 'started' else '' return self.driver.id if self.driver.status == 'started' else ''

View File

@ -97,6 +97,9 @@ class ChromiumBase(BasePage):
@property @property
def json(self) -> Union[dict, None]: ... def json(self) -> Union[dict, None]: ...
@property
def _target_id(self) -> str: ...
@property @property
def tab_id(self) -> str: ... def tab_id(self) -> str: ...

View File

@ -463,7 +463,7 @@ class ChromiumElement(DrissionElement):
node = self.page.run_cdp('DOM.describeNode', backendNodeId=self._backend_id)['node'] node = self.page.run_cdp('DOM.describeNode', backendNodeId=self._backend_id)['node']
frame = node.get('frameId', None) frame = node.get('frameId', None)
frame = frame or self.page.tab_id frame = frame or self.page._target_id
try: try:
result = self.page.run_cdp('Page.getResourceContent', frameId=frame, url=src) result = self.page.run_cdp('Page.getResourceContent', frameId=frame, url=src)

View File

@ -17,10 +17,15 @@ from .waiter import FrameWaiter
class ChromiumFrame(ChromiumBase): class ChromiumFrame(ChromiumBase):
def __init__(self, page, ele): def __init__(self, page, ele):
"""
:param page: frame所在的页面对象
:param ele: frame所在元素
"""
self.page = page self.page = page
self.address = page.address self.address = page.address
node = page.run_cdp('DOM.describeNode', backendNodeId=ele.ids.backend_id)['node'] node = page.run_cdp('DOM.describeNode', backendNodeId=ele.ids.backend_id)['node']
self.frame_id = node['frameId'] self.frame_id = node['frameId']
self._tab_id = page.tab_id
self._backend_id = ele.ids.backend_id self._backend_id = ele.ids.backend_id
self._frame_ele = ele self._frame_ele = ele
self._states = None self._states = None
@ -329,6 +334,11 @@ class ChromiumFrame(ChromiumBase):
self._wait = FrameWaiter(self) self._wait = FrameWaiter(self)
return self._wait return self._wait
@property
def tab_id(self):
"""返回frame所在tab的id"""
return self._tab_id
def refresh(self): def refresh(self):
"""刷新frame页面""" """刷新frame页面"""
self._check_ok() self._check_ok()
@ -616,7 +626,7 @@ class ChromiumFrameIds(object):
@property @property
def tab_id(self): def tab_id(self):
"""返回当前标签页id""" """返回当前标签页id"""
return self._frame.page.tab_id return self._frame._tab_id
@property @property
def backend_id(self): def backend_id(self):

View File

@ -114,6 +114,9 @@ class ChromiumFrame(ChromiumBase):
@property @property
def wait(self) -> FrameWaiter: ... def wait(self) -> FrameWaiter: ...
@property
def tab_id(self) -> str: ...
def refresh(self) -> None: ... def refresh(self) -> None: ...
def attr(self, attr: str) -> Union[str, None]: ... def attr(self, attr: str) -> Union[str, None]: ...

View File

@ -99,10 +99,6 @@ class ChromiumPage(ChromiumBase):
self._rect = None self._rect = None
self._main_tab = self.tab_id self._main_tab = self.tab_id
# try:
# self.download_set.by_browser()
# except CDPError:
# pass
self._process_id = None self._process_id = None
r = self.browser_driver.call_method('SystemInfo.getProcessInfo') 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'] 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): # class BaseDownloadSetter(DownloadSetter):
# """用于设置下载参数的类""" # """用于设置下载参数的类"""
# #

View File

@ -8,6 +8,7 @@ from copy import copy
from .chromium_base import ChromiumBase from .chromium_base import ChromiumBase
from .commons.web import set_session_cookies, set_browser_cookies from .commons.web import set_session_cookies, set_browser_cookies
from .session_page import SessionPage from .session_page import SessionPage
from .setter import TabSetter
from .setter import WebPageTabSetter from .setter import WebPageTabSetter
@ -28,6 +29,7 @@ class ChromiumTab(ChromiumBase):
self.retry_times = self.page.retry_times self.retry_times = self.page.retry_times
self.retry_interval = self.page.retry_interval self.retry_interval = self.page.retry_interval
self._page_load_strategy = self.page.page_load_strategy self._page_load_strategy = self.page.page_load_strategy
self._download_path = self.page.download_path
def close(self): def close(self):
"""关闭当前标签页""" """关闭当前标签页"""
@ -38,6 +40,13 @@ class ChromiumTab(ChromiumBase):
"""返回获取窗口坐标和大小的对象""" """返回获取窗口坐标和大小的对象"""
return self.page.rect return self.page.rect
@property
def set(self):
"""返回用于等待的对象"""
if self._set is None:
self._set = TabSetter(self)
return self._set
class WebPageTab(SessionPage, ChromiumTab): class WebPageTab(SessionPage, ChromiumTab):
def __init__(self, page, tab_id): def __init__(self, page, tab_id):

View File

@ -13,6 +13,7 @@ from .chromium_frame import ChromiumFrame
from .chromium_page import ChromiumPage, ChromiumTabRect from .chromium_page import ChromiumPage, ChromiumTabRect
from .session_element import SessionElement from .session_element import SessionElement
from .session_page import SessionPage from .session_page import SessionPage
from .setter import TabSetter
from .setter import WebPageTabSetter from .setter import WebPageTabSetter
from .web_page import WebPage from .web_page import WebPage
@ -29,6 +30,9 @@ class ChromiumTab(ChromiumBase):
@property @property
def rect(self) -> ChromiumTabRect: ... def rect(self) -> ChromiumTabRect: ...
@property
def set(self) -> TabSetter: ...
class WebPageTab(SessionPage, ChromiumTab): class WebPageTab(SessionPage, ChromiumTab):
def __init__(self, page: WebPage, tab_id: str): def __init__(self, page: WebPage, tab_id: str):

View File

@ -118,7 +118,23 @@ class ChromiumBaseSetter(object):
self._page.run_cdp('Network.setExtraHTTPHeaders', headers=headers) 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): def main_tab(self, tab_id=None):
"""设置主tab """设置主tab
:param tab_id: 标签页id不传入则设置当前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}') 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): def __init__(self, page):
"""
:param page: SessionPage对象
"""
self._page = page self._page = page
def retry_times(self, times): def retry_times(self, times):
@ -155,6 +174,15 @@ class SessionPageSetter(object):
"""设置连接失败时重连间隔""" """设置连接失败时重连间隔"""
self._page.retry_interval = interval 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): def timeout(self, second):
"""设置连接超时时间 """设置连接超时时间
:param second: 秒数 :param second: 秒数
@ -274,7 +302,7 @@ class SessionPageSetter(object):
self._page.session.mount(url, adapter) self._page.session.mount(url, adapter)
class WebPageSetter(ChromiumPageSetter): class WebPageSetter(ChromiumPageSetter, DownloadSetter):
def __init__(self, page): def __init__(self, page):
super().__init__(page) super().__init__(page)
self._session_setter = SessionPageSetter(self._page) self._session_setter = SessionPageSetter(self._page)
@ -308,7 +336,7 @@ class WebPageSetter(ChromiumPageSetter):
self._chromium_setter.user_agent(ua, platform) self._chromium_setter.user_agent(ua, platform)
class WebPageTabSetter(ChromiumBaseSetter): class WebPageTabSetter(ChromiumBaseSetter, DownloadSetter):
def __init__(self, page): def __init__(self, page):
super().__init__(page) super().__init__(page)
self._session_setter = SessionPageSetter(self._page) self._session_setter = SessionPageSetter(self._page)

View File

@ -4,6 +4,7 @@
@Contact : g1879@qq.com @Contact : g1879@qq.com
""" """
from http.cookiejar import Cookie from http.cookiejar import Cookie
from pathlib import Path
from typing import Union, Tuple from typing import Union, Tuple
from requests.adapters import HTTPAdapter from requests.adapters import HTTPAdapter
@ -50,7 +51,15 @@ class ChromiumBaseSetter(object):
def upload_files(self, files: Union[str, list, tuple]) -> None: ... 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 = ... _page: ChromiumPage = ...
def main_tab(self, tab_id: str = None) -> None: ... 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: ... def tab_to_front(self, tab_or_id: Union[str, ChromiumTab] = None) -> None: ...
class SessionPageSetter(object): class SessionPageSetter(DownloadSetter):
def __init__(self, page: SessionPage): def __init__(self, page: SessionPage):
self._page: SessionPage = ... self._page: SessionPage = ...
@ -102,7 +111,7 @@ class SessionPageSetter(object):
def add_adapter(self, url: str, adapter: HTTPAdapter) -> None: ... def add_adapter(self, url: str, adapter: HTTPAdapter) -> None: ...
class WebPageSetter(ChromiumPageSetter): class WebPageSetter(ChromiumPageSetter, DownloadSetter):
_page: WebPage = ... _page: WebPage = ...
_session_setter: SessionPageSetter = ... _session_setter: SessionPageSetter = ...
_chromium_setter: ChromiumPageSetter = ... _chromium_setter: ChromiumPageSetter = ...
@ -114,7 +123,7 @@ class WebPageSetter(ChromiumPageSetter):
def cookies(self, cookies) -> None: ... def cookies(self, cookies) -> None: ...
class WebPageTabSetter(ChromiumBaseSetter): class WebPageTabSetter(ChromiumBaseSetter, DownloadSetter):
_page: WebPage = ... _page: WebPage = ...
_session_setter: SessionPageSetter = ... _session_setter: SessionPageSetter = ...
_chromium_setter: ChromiumBaseSetter = ... _chromium_setter: ChromiumBaseSetter = ...