mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
继续完善
This commit is contained in:
parent
06877903cd
commit
ea8321990c
@ -38,7 +38,7 @@ class ChromiumPage(BasePage):
|
|||||||
def _connect_browser(self, addr_tab_opts: Union[str, Tab, DriverOptions] = None,
|
def _connect_browser(self, addr_tab_opts: Union[str, Tab, DriverOptions] = None,
|
||||||
tab_id: str = None) -> None:
|
tab_id: str = None) -> None:
|
||||||
"""连接浏览器,在第一次时运行 \n
|
"""连接浏览器,在第一次时运行 \n
|
||||||
:param addr_tab_opts: Tab对象或DriverOptions对象
|
:param addr_tab_opts: 浏览器地址、Tab对象或DriverOptions对象
|
||||||
:param tab_id: 要控制的标签页id,不指定默认为激活的
|
:param tab_id: 要控制的标签页id,不指定默认为激活的
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
@ -83,7 +83,7 @@ class ChromiumPage(BasePage):
|
|||||||
self.process = None
|
self.process = None
|
||||||
self.options = DriverOptions(read_file=False)
|
self.options = DriverOptions(read_file=False)
|
||||||
self._set_options()
|
self._set_options()
|
||||||
self._init_page(None)
|
self._init_page(tab_id)
|
||||||
self._get_document()
|
self._get_document()
|
||||||
self._first_run = False
|
self._first_run = False
|
||||||
|
|
||||||
@ -130,14 +130,14 @@ class ChromiumPage(BasePage):
|
|||||||
|
|
||||||
end_time = perf_counter() + timeout
|
end_time = perf_counter() + timeout
|
||||||
while perf_counter() < end_time:
|
while perf_counter() < end_time:
|
||||||
state = self._driver.Runtime.evaluate(expression='document.readyState;')['result']['value']
|
state = self.ready_state
|
||||||
if state == 'complete':
|
if state == 'complete':
|
||||||
return True
|
return True
|
||||||
elif self.page_load_strategy == 'eager' and state in ('interactive', 'complete'):
|
elif self.page_load_strategy == 'eager' and state in ('interactive', 'complete'):
|
||||||
self._driver.Page.stopLoading()
|
self.stop_loading()
|
||||||
return True
|
return True
|
||||||
elif self.page_load_strategy == 'none':
|
elif self.page_load_strategy == 'none':
|
||||||
self._driver.Page.stopLoading()
|
self.stop_loading()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
@ -145,13 +145,14 @@ class ChromiumPage(BasePage):
|
|||||||
def _onLoadEventFired(self, **kwargs):
|
def _onLoadEventFired(self, **kwargs):
|
||||||
"""在页面刷新、变化后重新读取页面内容"""
|
"""在页面刷新、变化后重新读取页面内容"""
|
||||||
# print('load complete')
|
# print('load complete')
|
||||||
if self._first_run is False:
|
if self._first_run is False and self._is_loading:
|
||||||
self._get_document()
|
self._get_document()
|
||||||
|
|
||||||
def _onFrameNavigated(self, **kwargs):
|
def _onFrameNavigated(self, **kwargs):
|
||||||
# print('nav')
|
"""页面跳转时触发"""
|
||||||
# todo: 考虑frame的情况,修改别的判断方式
|
# todo: 考虑frame的情况,修改别的判断方式
|
||||||
if not kwargs['frame'].get('parentId', None):
|
if not kwargs['frame'].get('parentId', None):
|
||||||
|
# print('nav')
|
||||||
self._is_loading = True
|
self._is_loading = True
|
||||||
|
|
||||||
def _onDocumentUpdated(self, **kwargs):
|
def _onDocumentUpdated(self, **kwargs):
|
||||||
@ -159,6 +160,7 @@ class ChromiumPage(BasePage):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def _set_options(self) -> None:
|
def _set_options(self) -> None:
|
||||||
|
print(self.options.timeouts)
|
||||||
self.set_timeouts(page_load=self.options.timeouts['pageLoad'] / 1000,
|
self.set_timeouts(page_load=self.options.timeouts['pageLoad'] / 1000,
|
||||||
script=self.options.timeouts['script'] / 1000,
|
script=self.options.timeouts['script'] / 1000,
|
||||||
implicit=self.options.timeouts['implicit'] / 1000 if self.timeout is None else self.timeout)
|
implicit=self.options.timeouts['implicit'] / 1000 if self.timeout is None else self.timeout)
|
||||||
@ -220,7 +222,7 @@ class ChromiumPage(BasePage):
|
|||||||
@property
|
@property
|
||||||
def ready_state(self) -> str:
|
def ready_state(self) -> str:
|
||||||
"""返回当前页面加载状态,'loading' 'interactive' 'complete'"""
|
"""返回当前页面加载状态,'loading' 'interactive' 'complete'"""
|
||||||
return self.run_script('document.readyState;', as_expr=True)
|
return self._driver.Runtime.evaluate(expression='document.readyState;')['result']['value']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def size(self) -> dict:
|
def size(self) -> dict:
|
||||||
@ -510,7 +512,7 @@ class ChromiumPage(BasePage):
|
|||||||
:param ignore_cache: 是否忽略缓存
|
:param ignore_cache: 是否忽略缓存
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
self.driver.Page.reload(ignoreCache=ignore_cache)
|
self._driver.Page.reload(ignoreCache=ignore_cache)
|
||||||
|
|
||||||
def forward(self, steps: int = 1) -> None:
|
def forward(self, steps: int = 1) -> None:
|
||||||
"""在浏览历史中前进若干步 \n
|
"""在浏览历史中前进若干步 \n
|
||||||
@ -528,7 +530,8 @@ class ChromiumPage(BasePage):
|
|||||||
|
|
||||||
def stop_loading(self) -> None:
|
def stop_loading(self) -> None:
|
||||||
"""页面停止加载"""
|
"""页面停止加载"""
|
||||||
self.driver.Page.stopLoading()
|
self._driver.Page.stopLoading()
|
||||||
|
self._get_document()
|
||||||
|
|
||||||
def run_cdp(self, cmd: str, **cmd_args) -> dict:
|
def run_cdp(self, cmd: str, **cmd_args) -> dict:
|
||||||
"""执行Chrome DevTools Protocol语句 \n
|
"""执行Chrome DevTools Protocol语句 \n
|
||||||
@ -536,7 +539,7 @@ class ChromiumPage(BasePage):
|
|||||||
:param cmd_args: 参数
|
:param cmd_args: 参数
|
||||||
:return: 执行的结果
|
:return: 执行的结果
|
||||||
"""
|
"""
|
||||||
return self.driver.call_method(cmd, **cmd_args)
|
return self._driver.call_method(cmd, **cmd_args)
|
||||||
|
|
||||||
def set_user_agent(self, ua: str) -> None:
|
def set_user_agent(self, ua: str) -> None:
|
||||||
"""为当前tab设置user agent,只在当前tab有效 \n
|
"""为当前tab设置user agent,只在当前tab有效 \n
|
||||||
@ -622,8 +625,7 @@ class ChromiumPage(BasePage):
|
|||||||
|
|
||||||
self._driver.stop()
|
self._driver.stop()
|
||||||
self._init_page(tab_id)
|
self._init_page(tab_id)
|
||||||
state = self._driver.Runtime.evaluate(expression='document.readyState;')['result']['value']
|
if self.ready_state == 'complete':
|
||||||
if state == 'complete':
|
|
||||||
self._get_document()
|
self._get_document()
|
||||||
|
|
||||||
def close_tabs(self, tab_ids: Union[str, List[str], Tuple[str]] = None, others: bool = False) -> None:
|
def close_tabs(self, tab_ids: Union[str, List[str], Tuple[str]] = None, others: bool = False) -> None:
|
||||||
@ -696,9 +698,9 @@ class ChromiumPage(BasePage):
|
|||||||
|
|
||||||
res_text = self._alert.text
|
res_text = self._alert.text
|
||||||
if self._alert.type == 'prompt':
|
if self._alert.type == 'prompt':
|
||||||
self.driver.Page.handleJavaScriptDialog(accept=accept, promptText=send)
|
self._driver.Page.handleJavaScriptDialog(accept=accept, promptText=send)
|
||||||
else:
|
else:
|
||||||
self.driver.Page.handleJavaScriptDialog(accept=accept)
|
self._driver.Page.handleJavaScriptDialog(accept=accept)
|
||||||
return res_text
|
return res_text
|
||||||
|
|
||||||
def hide_browser(self) -> None:
|
def hide_browser(self) -> None:
|
||||||
@ -732,15 +734,13 @@ class ChromiumPage(BasePage):
|
|||||||
timeout = timeout if timeout is not None else self.timeouts.page_load
|
timeout = timeout if timeout is not None else self.timeouts.page_load
|
||||||
|
|
||||||
for _ in range(times + 1):
|
for _ in range(times + 1):
|
||||||
result = self.driver.Page.navigate(url=to_url)
|
result = self._driver.Page.navigate(url=to_url)
|
||||||
is_timeout = not self._wait_loading(timeout)
|
is_timeout = not self._wait_loading(timeout)
|
||||||
|
|
||||||
if is_timeout:
|
if is_timeout:
|
||||||
err = TimeoutError('页面连接超时。')
|
err = TimeoutError('页面连接超时。')
|
||||||
continue
|
|
||||||
if 'errorText' in result:
|
if 'errorText' in result:
|
||||||
err = ConnectionError(result['errorText'])
|
err = ConnectionError(result['errorText'])
|
||||||
continue
|
|
||||||
|
|
||||||
if not err:
|
if not err:
|
||||||
break
|
break
|
||||||
@ -803,7 +803,7 @@ class WindowSizeSetter(object):
|
|||||||
"""用于设置窗口大小的类"""
|
"""用于设置窗口大小的类"""
|
||||||
|
|
||||||
def __init__(self, page: ChromiumPage):
|
def __init__(self, page: ChromiumPage):
|
||||||
self.driver = page.driver
|
self.driver = page._driver
|
||||||
self.window_id = self._get_info()['windowId']
|
self.window_id = self._get_info()['windowId']
|
||||||
|
|
||||||
def maximized(self) -> None:
|
def maximized(self) -> None:
|
||||||
|
@ -464,7 +464,7 @@ class DriverOptions(Options):
|
|||||||
self._driver_path = None
|
self._driver_path = None
|
||||||
self._user_data_path = None
|
self._user_data_path = None
|
||||||
self.ini_path = None
|
self.ini_path = None
|
||||||
self.timeouts = {'implicit': 10, 'pageLoad': 30, 'script': 30}
|
self.timeouts = {'implicit': 10000, 'pageLoad': 30000, 'script': 30000}
|
||||||
|
|
||||||
if read_file:
|
if read_file:
|
||||||
self.ini_path = ini_path or str(Path(__file__).parent / 'configs.ini')
|
self.ini_path = ini_path or str(Path(__file__).parent / 'configs.ini')
|
||||||
|
@ -35,7 +35,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
|
|
||||||
super(ChromiumPage, self).__init__(timeout) # 调用Base的__init__()
|
super(ChromiumPage, self).__init__(timeout) # 调用Base的__init__()
|
||||||
self._session = None
|
self._session = None
|
||||||
self._driver = None
|
self._tab_obj = None
|
||||||
|
self._is_loading = False
|
||||||
self._set_session_options(session_or_options)
|
self._set_session_options(session_or_options)
|
||||||
self._set_driver_options(driver_or_options)
|
self._set_driver_options(driver_or_options)
|
||||||
self._setting_tab_id = tab_id
|
self._setting_tab_id = tab_id
|
||||||
@ -43,7 +44,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
self._response = None
|
self._response = None
|
||||||
|
|
||||||
if self._mode == 'd':
|
if self._mode == 'd':
|
||||||
d = self.driver
|
self.driver
|
||||||
|
|
||||||
def __call__(self,
|
def __call__(self,
|
||||||
loc_or_str: Union[Tuple[str, str], str, ChromiumElement, SessionElement],
|
loc_or_str: Union[Tuple[str, str], str, ChromiumElement, SessionElement],
|
||||||
@ -74,7 +75,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
if self._mode == 's':
|
if self._mode == 's':
|
||||||
return super().html
|
return super().html
|
||||||
elif self._mode == 'd':
|
elif self._mode == 'd':
|
||||||
return super(SessionPage, self).html
|
return super(SessionPage, self).html if self._has_driver else ''
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def json(self) -> dict:
|
def json(self) -> dict:
|
||||||
@ -113,15 +114,23 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
|
|
||||||
return self._session
|
return self._session
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _driver(self) -> Tab:
|
||||||
|
"""返回纯粹的Tab对象,调用时切换到d模式,并连接浏览器"""
|
||||||
|
self.change_mode('d')
|
||||||
|
if self._tab_obj is None:
|
||||||
|
self._connect_browser(self._driver_options, self._setting_tab_id)
|
||||||
|
return self._tab_obj
|
||||||
|
|
||||||
|
@_driver.setter
|
||||||
|
def _driver(self, tab):
|
||||||
|
self._tab_obj = tab
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def driver(self) -> Tab:
|
def driver(self) -> Tab:
|
||||||
"""返回Tab对象,如未初始化则按配置信息创建。 \n
|
"""返回Tab对象,如未初始化则按配置信息创建。 \n
|
||||||
如设置了本地调试浏览器,可自动接入或打开浏览器进程。
|
如设置了本地调试浏览器,可自动接入或打开浏览器进程。
|
||||||
"""
|
"""
|
||||||
self.change_mode('d')
|
|
||||||
if self._driver is None:
|
|
||||||
self._connect_browser(self._driver_options, self._setting_tab_id)
|
|
||||||
|
|
||||||
return super().driver
|
return super().driver
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -209,7 +218,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
|
|||||||
return
|
return
|
||||||
|
|
||||||
self._mode = 's' if self._mode == 'd' else 'd'
|
self._mode = 's' if self._mode == 'd' else 'd'
|
||||||
print(self._mode)
|
|
||||||
# s模式转d模式
|
# s模式转d模式
|
||||||
if self._mode == 'd':
|
if self._mode == 'd':
|
||||||
if not self._has_driver:
|
if not self._has_driver:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user