SessionPage删除timeout参数,未完成

This commit is contained in:
g1879 2024-06-28 17:41:16 +08:00
parent 9f49f874ca
commit ac3a8ec27c
8 changed files with 31 additions and 38 deletions

View File

@ -317,9 +317,9 @@ class Browser(object):
elif Settings.singleton_tab_obj: elif Settings.singleton_tab_obj:
return id_or_num return id_or_num
else: else:
return self._get_tab(id_or_num.tab_id) # todo: 循环调用 return self._get_tab(id_or_num.tab_id) # fixme: 这里会出现循环调用
elif title == url == tab_type is None: elif title == url is None and tab_type == 'page':
id_or_num = self.tab_ids[0] id_or_num = self.tab_ids[0]
else: else:

View File

@ -38,12 +38,13 @@ __ERROR__ = 'error'
class ChromiumBase(BasePage): class ChromiumBase(BasePage):
"""标签页、frame、页面基类""" """标签页、frame、页面基类"""
def __init__(self, address, tab_id=None): def __init__(self, browser, tab_id=None):
""" """
:param address: 浏览器 ip:port :param browser: Browser
:param tab_id: 要控制的标签页id不指定默认为激活的 :param tab_id: 要控制的标签页id不指定默认为激活的
""" """
super().__init__() super().__init__()
self._browser = browser
self._is_loading = None self._is_loading = None
self._root_id = None # object id self._root_id = None # object id
self._set = None self._set = None
@ -64,19 +65,19 @@ class ChromiumBase(BasePage):
if not hasattr(self, '_listener'): if not hasattr(self, '_listener'):
self._listener = None self._listener = None
if isinstance(address, int) or (isinstance(address, str) and address.isdigit()): # if isinstance(address, int) or (isinstance(address, str) and address.isdigit()):
address = f'127.0.0.1:{address}' # address = f'127.0.0.1:{address}'
self._d_set_start_options(address) # self._d_set_start_options(address)
self._d_set_runtime_settings() self._d_set_runtime_settings()
self._connect_browser(tab_id) self._connect_browser(tab_id)
def _d_set_start_options(self, address): # def _d_set_start_options(self, address):
"""设置浏览器启动属性 # """设置浏览器启动属性
:param address: 'ip:port' # :param address: 'ip:port'
:return: None # :return: None
""" # """
self.address = address.replace('localhost', '127.0.0.1').lstrip('http://').lstrip('https://') # self.address = address.replace('localhost', '127.0.0.1').lstrip('http://').lstrip('https://')
def _d_set_runtime_settings(self): def _d_set_runtime_settings(self):
self._timeouts = Timeout(self) self._timeouts = Timeout(self)
@ -90,7 +91,7 @@ class ChromiumBase(BasePage):
self._is_reading = False self._is_reading = False
if not tab_id: if not tab_id:
tabs = self.browser._driver.get(f'http://{self.address}/json').json() tabs = self.browser._driver.get(f'http://{self.browser.address}/json').json()
tabs = [(i['id'], i['url']) for i in tabs tabs = [(i['id'], i['url']) for i in tabs
if i['type'] in ('page', 'webview') and not i['url'].startswith('devtools://')] if i['type'] in ('page', 'webview') and not i['url'].startswith('devtools://')]
dialog = None dialog = None

View File

@ -31,12 +31,10 @@ PIC_TYPE = Literal['jpg', 'jpeg', 'png', 'webp', True]
class ChromiumBase(BasePage): class ChromiumBase(BasePage):
def __init__(self, def __init__(self,
address: Union[str, int], browser: Browser,
tab_id: str = None): tab_id: str = None):
self._browser: Browser = ... self._browser: Browser = ...
self._page: ChromiumPage = ...
self.tab: Union[ChromiumPage, ChromiumTab] = ... self.tab: Union[ChromiumPage, ChromiumTab] = ...
self.address: str = ...
self._driver: Driver = ... self._driver: Driver = ...
self._frame_id: str = ... self._frame_id: str = ...
self._is_reading: bool = ... self._is_reading: bool = ...
@ -90,7 +88,7 @@ class ChromiumBase(BasePage):
def _wait_to_stop(self): ... def _wait_to_stop(self): ...
def _d_set_start_options(self, address) -> None: ... # def _d_set_start_options(self, address) -> None: ...
def _d_set_runtime_settings(self) -> None: ... def _d_set_runtime_settings(self) -> None: ...

View File

@ -28,7 +28,6 @@ class ChromiumFrame(ChromiumBase):
:param info: frame所在元素信息 :param info: frame所在元素信息
""" """
self.tab = owner.tab self.tab = owner.tab
self._browser = owner.browser
self._target_page = owner self._target_page = owner
# if owner._type in ('ChromiumPage', 'WebPage'): # if owner._type in ('ChromiumPage', 'WebPage'):
# self._target_page = self.tab = owner # self._target_page = self.tab = owner
@ -39,8 +38,8 @@ class ChromiumFrame(ChromiumBase):
# self._target_page = owner # self._target_page = owner
# self.tab = owner.tab if owner._type == 'ChromiumFrame' else owner # self.tab = owner.tab if owner._type == 'ChromiumFrame' else owner
self.address = owner.address # self.address = owner.address
self._tab_id = owner.tab_id # self._tab_id = owner.tab_id
self._backend_id = ele._backend_id self._backend_id = ele._backend_id
self._frame_ele = ele self._frame_ele = ele
self._states = None self._states = None
@ -51,11 +50,11 @@ class ChromiumFrame(ChromiumBase):
if self._is_inner_frame(): if self._is_inner_frame():
self._is_diff_domain = False self._is_diff_domain = False
self.doc_ele = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId']) self.doc_ele = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId'])
super().__init__(owner.address, owner.tab_id, owner.timeout) super().__init__(owner.browser, owner.tab_id)
else: else:
self._is_diff_domain = True self._is_diff_domain = True
delattr(self, '_frame_id') delattr(self, '_frame_id')
super().__init__(owner.address, node['frameId'], owner.timeout) super().__init__(owner.browser, node['frameId'])
obj_id = super().run_js('document;', as_expr=True)['objectId'] obj_id = super().run_js('document;', as_expr=True)['objectId']
self.doc_ele = ChromiumElement(self, obj_id=obj_id) self.doc_ele = ChromiumElement(self, obj_id=obj_id)
@ -101,7 +100,7 @@ class ChromiumFrame(ChromiumBase):
try: try:
super()._driver_init(tab_id) super()._driver_init(tab_id)
except: except:
self.browser.driver.get(f'http://{self.address}/json') self.browser._driver.get(f'http://{self._browser.address}/json')
super()._driver_init(tab_id) super()._driver_init(tab_id)
self._driver.set_callback('Inspector.detached', self._onInspectorDetached, immediate=True) self._driver.set_callback('Inspector.detached', self._onInspectorDetached, immediate=True)
self._driver.set_callback('Page.frameDetached', None) self._driver.set_callback('Page.frameDetached', None)
@ -135,16 +134,16 @@ class ChromiumFrame(ChromiumBase):
self.doc_ele = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId']) self.doc_ele = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId'])
self._frame_id = node['frameId'] self._frame_id = node['frameId']
if self._listener: if self._listener:
self._listener._to_target(self._target_page.tab_id, self.address, self) self._listener._to_target(self._target_page.tab_id, self._browser.address, self)
super().__init__(self.address, self._target_page.tab_id, self._target_page.timeout) super().__init__(self._browser, self._target_page.tab_id)
# self.driver._debug = d_debug # self.driver._debug = d_debug
else: else:
self._is_diff_domain = True self._is_diff_domain = True
if self._listener: if self._listener:
self._listener._to_target(node['frameId'], self.address, self) self._listener._to_target(node['frameId'], self._browser.address, self)
end_time = perf_counter() + self.timeouts.page_load end_time = perf_counter() + self.timeouts.page_load
super().__init__(self.address, node['frameId'], self._target_page.timeout) super().__init__(self._browser, node['frameId'])
timeout = end_time - perf_counter() timeout = end_time - perf_counter()
if timeout <= 0: if timeout <= 0:
timeout = .5 timeout = .5
@ -323,7 +322,7 @@ class ChromiumFrame(ChromiumBase):
@property @property
def tab_id(self): def tab_id(self):
"""返回frame所在tab的id""" """返回frame所在tab的id"""
return self._tab_id return self.tab.tab_id
@property @property
def download_path(self): def download_path(self):

View File

@ -28,9 +28,8 @@ class ChromiumFrame(ChromiumBase):
owner: Union[ChromiumTab, ChromiumFrame], owner: Union[ChromiumTab, ChromiumFrame],
ele: ChromiumElement, ele: ChromiumElement,
info: dict = None): info: dict = None):
self._target_page: ChromiumBase = ... self._target_page: Union[ChromiumTab, ChromiumFrame] = ...
self.tab: ChromiumTab = ... self.tab: ChromiumTab = ...
# self._tab_id: str = ...
self._set: ChromiumFrameSetter = ... self._set: ChromiumFrameSetter = ...
self._frame_ele: ChromiumElement = ... self._frame_ele: ChromiumElement = ...
self._backend_id: int = ... self._backend_id: int = ...

View File

@ -47,7 +47,7 @@ class ChromiumTab(ChromiumBase):
self.tab = self self.tab = self
self._browser = browser self._browser = browser
super().__init__(browser.address, tab_id, browser.timeout) super().__init__(browser, tab_id)
self._rect = None self._rect = None
self._type = 'ChromiumTab' self._type = 'ChromiumTab'

View File

@ -25,10 +25,9 @@ from .._units.setter import SessionPageSetter
class SessionPage(BasePage): class SessionPage(BasePage):
"""SessionPage封装了页面操作的常用功能使用requests来获取、解析网页""" """SessionPage封装了页面操作的常用功能使用requests来获取、解析网页"""
def __init__(self, session_or_options=None, timeout=None): def __init__(self, session_or_options=None):
""" """
:param session_or_options: Session对象或SessionOptions对象 :param session_or_options: Session对象或SessionOptions对象
:param timeout: 连接超时时间为None时从ini文件读取或默认10
""" """
super(SessionPage, SessionPage).__init__(self) super(SessionPage, SessionPage).__init__(self)
self._headers = None self._headers = None
@ -41,8 +40,6 @@ class SessionPage(BasePage):
self._s_set_start_options(session_or_options) self._s_set_start_options(session_or_options)
self._s_set_runtime_settings() self._s_set_runtime_settings()
self._create_session() self._create_session()
if timeout is not None:
self.timeout = timeout
def _s_set_start_options(self, session_or_options): def _s_set_start_options(self, session_or_options):
"""启动配置 """启动配置

View File

@ -20,8 +20,7 @@ from .._units.setter import SessionPageSetter
class SessionPage(BasePage): class SessionPage(BasePage):
def __init__(self, def __init__(self,
session_or_options: Union[Session, SessionOptions] = None, session_or_options: Union[Session, SessionOptions] = None):
timeout: float = None):
self._headers: Optional[CaseInsensitiveDict] = ... self._headers: Optional[CaseInsensitiveDict] = ...
self._session: Session = ... self._session: Session = ...
self._session_options: SessionOptions = ... self._session_options: SessionOptions = ...