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 查找元素,无需切入切出 +- 可以跨`