3.1.3ChromiumBase的title、url、html等待加载结束才返回;wait_loading()默认使用页面timeout属性

This commit is contained in:
g1879 2023-02-02 23:35:57 +08:00
parent f3155df7a9
commit b5892c622b
3 changed files with 8 additions and 4 deletions

View File

@ -198,6 +198,7 @@ class ChromiumBase(BasePage):
@property @property
def title(self): def title(self):
"""返回当前页面title""" """返回当前页面title"""
self._wait_loaded()
return self._tab_obj.Target.getTargetInfo(targetId=self.tab_id)['targetInfo']['title'] return self._tab_obj.Target.getTargetInfo(targetId=self.tab_id)['targetInfo']['title']
@property @property
@ -225,11 +226,13 @@ class ChromiumBase(BasePage):
@property @property
def url(self): def url(self):
"""返回当前页面url""" """返回当前页面url"""
self._wait_loaded()
return self._tab_obj.Target.getTargetInfo(targetId=self.tab_id)['targetInfo']['url'] return self._tab_obj.Target.getTargetInfo(targetId=self.tab_id)['targetInfo']['url']
@property @property
def html(self): def html(self):
"""返回当前页面html文本""" """返回当前页面html文本"""
self._wait_loaded()
return self._wait_driver.DOM.getOuterHTML(objectId=self._root_id)['outerHTML'] return self._wait_driver.DOM.getOuterHTML(objectId=self._root_id)['outerHTML']
@property @property
@ -338,13 +341,13 @@ class ChromiumBase(BasePage):
timeout=timeout) timeout=timeout)
return self._url_available return self._url_available
def wait_loading(self, timeout=1): def wait_loading(self, timeout=None):
"""阻塞程序,等待页面进入加载状态 """阻塞程序,等待页面进入加载状态
:param timeout: 超时时间 :param timeout: 超时时间
:return: 等待结束时是否进入加载状态 :return: 等待结束时是否进入加载状态
""" """
if timeout: if timeout != 0:
timeout = 2 if timeout is True else timeout timeout = self.timeout if timeout in (None, True) else timeout
end_time = perf_counter() + timeout end_time = perf_counter() + timeout
while perf_counter() < end_time: while perf_counter() < end_time:
if self.is_loading: if self.is_loading:

View File

@ -318,6 +318,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
:param mode: 模式字符串 :param mode: 模式字符串
:param go: 是否跳转到原模式的url :param go: 是否跳转到原模式的url
:param copy_cookies: 是否复制cookies到目标模式 :param copy_cookies: 是否复制cookies到目标模式
:return: None
""" """
if mode is not None and mode.lower() == self._mode: if mode is not None and mode.lower() == self._mode:
return return

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup( setup(
name="DrissionPage", name="DrissionPage",
version="3.1.2", version="3.1.3",
author="g1879", author="g1879",
author_email="g1879@qq.com", author_email="g1879@qq.com",
description="A module that integrates selenium and requests session, encapsulates common page operations.", description="A module that integrates selenium and requests session, encapsulates common page operations.",