From 0268a9d9903c62b0e3853b1162ef622ee36382ff Mon Sep 17 00:00:00 2001 From: g1879 Date: Fri, 3 Feb 2023 18:14:22 +0800 Subject: [PATCH] =?UTF-8?q?ChromiumPage=E7=9A=84set=5Fuser=5Fagent()?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0platform=E5=8F=82=E6=95=B0=EF=BC=9BSessionPag?= =?UTF-8?q?e=E5=92=8CWebPage=E5=A2=9E=E5=8A=A0set=5Fuser=5Fagent()?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_base.py | 8 +- DrissionPage/chromium_base.pyi | 2 +- DrissionPage/session_page.py | 4 + DrissionPage/session_page.pyi | 2 + DrissionPage/web_page.py | 5 ++ DrissionPage/web_page.pyi | 2 + README.md | 6 +- docs/README.md | 2 + docs/_sidebar.md | 138 ++++++++++++++++----------------- docs/index.html | 11 +-- 10 files changed, 99 insertions(+), 81 deletions(-) diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 9ddcc9e..6a9d59f 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -567,12 +567,16 @@ class ChromiumBase(BasePage): raise RuntimeError('该元素已不在当前页面中。') raise - def set_user_agent(self, ua): + def set_user_agent(self, ua, platform=None): """为当前tab设置user agent,只在当前tab有效 :param ua: user agent字符串 + :param platform: platform字符串 :return: None """ - self._wait_driver.Network.setUserAgentOverride(userAgent=ua) + keys = {'userAgent': ua} + if platform: + keys['platform'] = platform + self._wait_driver.Emulation.setUserAgentOverride(**keys) def get_session_storage(self, item=None): """获取sessionStorage信息,不设置item则获取全部 diff --git a/DrissionPage/chromium_base.pyi b/DrissionPage/chromium_base.pyi index fe41e72..ce6cf69 100644 --- a/DrissionPage/chromium_base.pyi +++ b/DrissionPage/chromium_base.pyi @@ -169,7 +169,7 @@ class ChromiumBase(BasePage): def run_cdp(self, cmd: str, **cmd_args) -> dict: ... - def set_user_agent(self, ua: str) -> None: ... + def set_user_agent(self, ua: str, platform:str=None) -> None: ... def get_session_storage(self, item: str = None) -> Union[str, dict, None]: ... diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index 83c6f8e..0f79fa5 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -90,6 +90,10 @@ class SessionPage(BasePage): for i in headers: self.session.headers[i] = headers[i] + def set_user_agent(self, ua): + """设置user agent""" + self.session.headers['user-agent'] = ua + def __call__(self, loc_or_str, timeout=None): """在内部查找元素 例:ele2 = ele1('@id=ele_id') diff --git a/DrissionPage/session_page.pyi b/DrissionPage/session_page.pyi index 4c4f287..86bf66c 100644 --- a/DrissionPage/session_page.pyi +++ b/DrissionPage/session_page.pyi @@ -43,6 +43,8 @@ class SessionPage(BasePage): def set_headers(self, headers: dict) -> None: ... + def set_user_agent(self, ua: str) -> None: ... + def __call__(self, loc_or_str: Union[Tuple[str, str], str, SessionElement], timeout: float = None) -> Union[SessionElement, str, None]: ... diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index d4174d4..ab9dd94 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -389,6 +389,11 @@ class WebPage(SessionPage, ChromiumPage, BasePage): elif self._mode == 'd': return self._get_driver_cookies(as_dict) + def set_user_agent(self, ua, platform=None): + """设置user agent,d模式下只有当前tab有效""" + super().set_user_agent(ua) + super(SessionPage, self).set_user_agent(ua, platform) + def _get_driver_cookies(self, as_dict=False): """获取浏览器cookies :param as_dict: 以dict形式返回 diff --git a/DrissionPage/web_page.pyi b/DrissionPage/web_page.pyi index baec25d..025e85b 100644 --- a/DrissionPage/web_page.pyi +++ b/DrissionPage/web_page.pyi @@ -130,6 +130,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def get_cookies(self, as_dict: bool = False, all_domains: bool = False) -> Union[dict, list]: ... + def set_user_agent(self, ua: str, platform: str = None) -> None: ... + def _get_driver_cookies(self, as_dict: bool = False) -> dict: ... def set_cookies(self, cookies, set_session: bool = False, set_driver: bool = False) -> None: ... diff --git a/README.md b/README.md index 86f1c05..044cbaf 100644 --- a/README.md +++ b/README.md @@ -58,9 +58,9 @@ python 版本:3.6 及以上 - 运行速度更快 -- 可以跨 iframe 查找元素,无需切入切出 +- 可以跨`