page对象增加user_agent属性

This commit is contained in:
g1879 2023-05-26 18:37:00 +08:00
parent d9281914dd
commit 8a0b647b22
8 changed files with 40 additions and 2 deletions

View File

@ -335,6 +335,11 @@ class ChromiumBase(BasePage):
"""返回页面加载策略有3种'none''normal''eager'"""
return self._page_load_strategy
@property
def user_agent(self):
"""返回user agent"""
return self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
@property
def scroll(self):
"""返回用于滚动滚动条的对象"""

View File

@ -115,6 +115,9 @@ class ChromiumBase(BasePage):
@property
def page_load_strategy(self) -> str: ...
@property
def user_agent(self) -> str: ...
@property
def scroll(self) -> ChromiumPageScroll: ...

View File

@ -124,6 +124,14 @@ class WebPageTab(SessionPage, ChromiumTab):
"""以dict方式返回cookies"""
return super().cookies
@property
def user_agent(self):
"""返回user agent"""
if self._mode == 's':
return super().user_agent
elif self._mode == 'd':
return super(SessionPage, self).user_agent
@property
def session(self):
"""返回Session对象如未初始化则按配置信息创建"""

View File

@ -67,6 +67,9 @@ class WebPageTab(SessionPage, ChromiumTab):
@property
def cookies(self) -> dict: ...
@property
def user_agent(self) -> str: ...
@property
def session(self) -> Session: ...

View File

@ -98,6 +98,11 @@ class SessionPage(BasePage):
except Exception:
return None
@property
def user_agent(self):
"""返回user agent"""
return self.session.headers.get('user-agent', '')
@property
def download_path(self):
"""返回下载路径"""

View File

@ -60,6 +60,9 @@ class SessionPage(BasePage):
@property
def json(self) -> Union[dict, None]: ...
@property
def user_agent(self) -> str: ...
@property
def download_path(self) -> str: ...

View File

@ -182,6 +182,14 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
"""以dict方式返回cookies"""
return super().cookies
@property
def user_agent(self):
"""返回user agent"""
if self._mode == 's':
return super().user_agent
elif self._mode == 'd':
return super(SessionPage, self).user_agent
@property
def session(self):
"""返回Session对象如未初始化则按配置信息创建"""
@ -339,8 +347,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
return
if copy_user_agent:
selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
self.session.headers.update({"User-Agent": selenium_user_agent})
user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
self.session.headers.update({"User-Agent": user_agent})
set_session_cookies(self.session, self._get_driver_cookies())

View File

@ -66,6 +66,9 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
@property
def cookies(self) -> dict: ...
@property
def user_agent(self) -> str: ...
@property
def session(self) -> Session: ...