rect.borwser_size改为window_sizerect.borwser_location改为window_location;cdp设置30秒超时

This commit is contained in:
g1879 2023-10-30 22:05:52 +08:00
parent 989a92adb7
commit 722e299150
8 changed files with 58 additions and 56 deletions

View File

@ -155,7 +155,7 @@ class ChromiumDriver(object):
if self._stopped.is_set():
return {'error': 'tab closed', 'type': 'tab_closed'}
timeout = kwargs.pop("_timeout", None)
timeout = kwargs.pop("_timeout", 30)
result = self._send({"method": _method, "params": kwargs}, timeout=timeout)
if result is None:
return {'error': 'tab closed', 'type': 'tab_closed'}

View File

@ -145,28 +145,6 @@ class ChromiumBase(BasePage):
self._is_loading = False
self._is_reading = False
def _wait_loaded(self, timeout=None):
"""等待页面加载完成,超时触发停止加载
:param timeout: 超时时间
:return: 是否成功超时返回False
"""
if self.page_load_strategy == 'none':
return True
timeout = timeout if timeout is not None else self.timeouts.page_load
end_time = perf_counter() + timeout
while perf_counter() < end_time:
if self._ready_state == 'complete':
return True
elif self.page_load_strategy == 'eager' and self._ready_state in ('interactive', 'complete'):
self.stop_loading()
return True
sleep(.1)
self.stop_loading()
return False
def _onFrameDetached(self, **kwargs):
try:
self.browser._frames.pop(kwargs['frameId'])
@ -845,6 +823,28 @@ class ChromiumBase(BasePage):
self._alert.response_text = None
self._has_alert = True
def _wait_loaded(self, timeout=None):
"""等待页面加载完成,超时触发停止加载
:param timeout: 超时时间
:return: 是否成功超时返回False
"""
if self.page_load_strategy == 'none':
return True
timeout = timeout if timeout is not None else self.timeouts.page_load
end_time = perf_counter() + timeout
while perf_counter() < end_time:
if self._ready_state == 'complete':
return True
elif self.page_load_strategy == 'eager' and self._ready_state in ('interactive',
'complete') and not self._is_loading:
return True
sleep(.1)
self.stop_loading()
return False
def _d_connect(self, to_url, times=0, interval=1, show_errmsg=False, timeout=None):
"""尝试连接,重试若干次
:param to_url: 要访问的url

View File

@ -32,6 +32,7 @@ class ChromiumBase(BasePage):
self._driver: ChromiumDriver = ...
self._frame_id: str = ...
self._is_reading: bool = ...
self._is_timeout: bool = ...
self._timeouts: Timeout = ...
self._first_run: bool = ...
self._is_loading: bool = ...

View File

@ -35,7 +35,8 @@ class NetworkListener(object):
def go_on(self) -> None: ...
def wait(self, count: int = 1, timeout: float = None, fix_count: bool = True): ...
def wait(self, count: int = 1, timeout: float = None,
fix_count: bool = True) -> Union[List[DataPacket], DataPacket, None]: ...
@property
def results(self) -> Union[DataPacket, Dict[str, List[DataPacket]], False]: ...

View File

@ -122,6 +122,11 @@ class TabSetter(ChromiumBaseSetter):
def __init__(self, page):
super().__init__(page)
@property
def window(self):
"""返回用于设置浏览器窗口的对象"""
return WindowSetter(self._page)
def download_path(self, path):
"""设置下载路径
:param path: 下载路径
@ -165,11 +170,6 @@ class ChromiumPageSetter(TabSetter):
"""
self._page._main_tab = tab_id or self._page.tab_id
@property
def window(self):
"""返回用于设置浏览器窗口的对象"""
return WindowSetter(self._page)
def tab_to_front(self, tab_or_id=None):
"""激活标签页使其处于最前面
:param tab_or_id: 标签页对象或id为None表示当前标签页

View File

@ -56,6 +56,9 @@ class ChromiumBaseSetter(object):
class TabSetter(ChromiumBaseSetter):
def __init__(self, page): ...
@property
def window(self) -> WindowSetter: ...
def download_path(self, path: Union[str, Path]) -> None: ...
def download_file_name(self, name: str) -> None: ...
@ -70,9 +73,6 @@ class ChromiumPageSetter(TabSetter):
def main_tab(self, tab_id: str = None) -> None: ...
@property
def window(self) -> WindowSetter: ...
def tab_to_front(self, tab_or_id: Union[str, ChromiumTab] = None) -> None: ...
@ -184,8 +184,8 @@ class PageScrollSetter(object):
class WindowSetter(object):
def __init__(self, page: ChromiumPage):
self._page: ChromiumPage = ...
def __init__(self, page: ChromiumBase):
self._page: ChromiumBase = ...
self._window_id: str = ...
def maximized(self) -> None: ...

View File

@ -12,16 +12,27 @@ class ChromiumTabRect(object):
@property
def window_state(self):
"""返回窗口状态normal、fullscreen、maximized、 minimized"""
return self._get_browser_rect()['windowState']
return self._get_window_rect()['windowState']
@property
def browser_location(self):
"""返回浏览器在屏幕上的坐标,左上角为(0, 0)"""
r = self._get_browser_rect()
def window_location(self):
"""返回窗口在屏幕上的坐标,左上角为(0, 0)"""
r = self._get_window_rect()
if r['windowState'] in ('maximized', 'fullscreen'):
return 0, 0
return r['left'] + 7, r['top']
@property
def window_size(self):
"""返回窗口大小"""
r = self._get_window_rect()
if r['windowState'] == 'fullscreen':
return r['width'], r['height']
elif r['windowState'] == 'maximized':
return r['width'] - 16, r['height'] - 16
else:
return r['width'] - 16, r['height'] - 7
@property
def page_location(self):
"""返回页面左上角在屏幕中坐标,左上角为(0, 0)"""
@ -32,22 +43,11 @@ class ChromiumTabRect(object):
@property
def viewport_location(self):
"""返回视口在屏幕中坐标,左上角为(0, 0)"""
w_bl, h_bl = self.browser_location
w_bs, h_bs = self.browser_size
w_bl, h_bl = self.window_location
w_bs, h_bs = self.window_size
w_vs, h_vs = self.viewport_size_with_scrollbar
return w_bl + w_bs - w_vs, h_bl + h_bs - h_vs
@property
def browser_size(self):
"""返回浏览器大小"""
r = self._get_browser_rect()
if r['windowState'] == 'fullscreen':
return r['width'], r['height']
elif r['windowState'] == 'maximized':
return r['width'] - 16, r['height'] - 16
else:
return r['width'] - 16, r['height'] - 7
@property
def page_size(self):
"""返回页面总宽高,格式:(宽, 高)"""
@ -71,6 +71,6 @@ class ChromiumTabRect(object):
"""获取页面范围信息"""
return self._page.run_cdp_loaded('Page.getLayoutMetrics')
def _get_browser_rect(self):
"""获取浏览器范围信息"""
def _get_window_rect(self):
"""获取窗口范围信息"""
return self._page.browser.get_window_bounds(self._page.tab_id)

View File

@ -17,7 +17,7 @@ class ChromiumTabRect(object):
def window_state(self) -> str: ...
@property
def browser_location(self) -> Tuple[int, int]: ...
def window_location(self) -> Tuple[int, int]: ...
@property
def page_location(self) -> Tuple[int, int]: ...
@ -26,7 +26,7 @@ class ChromiumTabRect(object):
def viewport_location(self) -> Tuple[int, int]: ...
@property
def browser_size(self) -> Tuple[int, int]: ...
def window_size(self) -> Tuple[int, int]: ...
@property
def page_size(self) -> Tuple[int, int]: ...
@ -39,4 +39,4 @@ class ChromiumTabRect(object):
def _get_page_rect(self) -> dict: ...
def _get_browser_rect(self) -> dict: ...
def _get_window_rect(self) -> dict: ...