mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
用_target_id取代tab_id,frame对象能正确返回tab id;修复tab没有继承page下载路径的问题;page和tab对象添加set.download_path
This commit is contained in:
parent
fa918f105a
commit
8d89e4f7ef
@ -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
|
||||
|
@ -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 ''
|
||||
|
||||
|
@ -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: ...
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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):
|
||||
|
@ -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]: ...
|
||||
|
@ -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):
|
||||
# """用于设置下载参数的类"""
|
||||
#
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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)
|
||||
|
@ -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 = ...
|
||||
|
Loading…
x
Reference in New Issue
Block a user