diff --git a/DrissionPage/chromium_tab.py b/DrissionPage/chromium_tab.py index 814af68..3ada2b8 100644 --- a/DrissionPage/chromium_tab.py +++ b/DrissionPage/chromium_tab.py @@ -299,7 +299,7 @@ class WebPageTab(SessionPage, ChromiumTab): # set_session_cookies(self.session, self._get_driver_cookies(as_dict=True)) # set_session_cookies(self.session, self._get_driver_cookies(all_domains=True)) - set_session_cookies(self.session, self._get_driver_cookies()) + set_session_cookies(self.session, super(SessionPage, self).get_cookies()) def cookies_to_browser(self): """把session对象的cookies复制到浏览器""" @@ -320,22 +320,23 @@ class WebPageTab(SessionPage, ChromiumTab): if self._mode == 's': return super().get_cookies(as_dict, all_domains, all_info) elif self._mode == 'd': - return self._get_driver_cookies(as_dict, all_info) + # return self._get_driver_cookies(as_dict, all_info) + return super(SessionPage, self).get_cookies(as_dict, all_domains, all_info) - def _get_driver_cookies(self, as_dict=False, all_info=False): - """获取浏览器cookies - :param as_dict: 是否以dict形式返回,为True时all_info无效 - :param all_info: 是否返回所有信息,为False时只返回name、value、domain - :return: cookies信息 - """ - cookies = self.run_cdp('Network.getCookies')['cookies'] - if as_dict: - return {cookie['name']: cookie['value'] for cookie in cookies} - elif all_info: - return cookies - else: - return [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']} - for cookie in cookies] + # def _get_driver_cookies(self, as_dict=False, all_info=False): + # """获取浏览器cookies + # :param as_dict: 是否以dict形式返回,为True时all_info无效 + # :param all_info: 是否返回所有信息,为False时只返回name、value、domain + # :return: cookies信息 + # """ + # cookies = self.run_cdp('Network.getCookies')['cookies'] + # if as_dict: + # return {cookie['name']: cookie['value'] for cookie in cookies} + # elif all_info: + # return cookies + # else: + # return [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']} + # for cookie in cookies] def _find_elements(self, loc_or_ele, timeout=None, single=True, relative=False, raise_err=None): """返回页面中符合条件的元素、属性或节点文本,默认返回第一个 diff --git a/DrissionPage/chromium_tab.pyi b/DrissionPage/chromium_tab.pyi index a2dbe8a..9f42362 100644 --- a/DrissionPage/chromium_tab.pyi +++ b/DrissionPage/chromium_tab.pyi @@ -124,7 +124,7 @@ class WebPageTab(SessionPage, ChromiumTab): def get_cookies(self, as_dict: bool = False, all_domains: bool = False, all_info: bool = False) -> Union[dict, list]: ... - def _get_driver_cookies(self, as_dict: bool = False, all_info: bool = False) -> dict: ... + # def _get_driver_cookies(self, as_dict: bool = False, all_info: bool = False) -> dict: ... # ----------------重写SessionPage的函数----------------------- def post(self, diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index 55b44bf..57b01bd 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -350,7 +350,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage): 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()) + # set_session_cookies(self.session, self._get_driver_cookies()) + set_session_cookies(self.session, super(SessionPage, self).get_cookies()) def cookies_to_browser(self): """把session对象的cookies复制到浏览器""" @@ -371,7 +372,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage): if self._mode == 's': return super().get_cookies(as_dict, all_domains, all_info) elif self._mode == 'd': - return self._get_driver_cookies(as_dict, all_info) + return super(SessionPage, self).get_cookies(as_dict, all_domains, all_info) + # return self._get_driver_cookies(as_dict, all_info) def get_tab(self, tab_id=None): """获取一个标签页对象 @@ -381,20 +383,20 @@ class WebPage(SessionPage, ChromiumPage, BasePage): tab_id = tab_id or self.tab_id return WebPageTab(self, tab_id) - def _get_driver_cookies(self, as_dict=False, all_info=False): - """获取浏览器cookies - :param as_dict: 是否以dict形式返回,为True时all_info无效 - :param all_info: 是否返回所有信息 - :return: cookies信息 - """ - cookies = self.run_cdp('Network.getCookies')['cookies'] - if as_dict: - return {cookie['name']: cookie['value'] for cookie in cookies} - elif all_info: - return cookies - else: - return [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']} - for cookie in cookies] + # def _get_driver_cookies(self, as_dict=False, all_info=False): + # """获取浏览器cookies + # :param as_dict: 是否以dict形式返回,为True时all_info无效 + # :param all_info: 是否返回所有信息 + # :return: cookies信息 + # """ + # cookies = self.run_cdp('Network.getCookies')['cookies'] + # if as_dict: + # return {cookie['name']: cookie['value'] for cookie in cookies} + # elif all_info: + # return cookies + # else: + # return [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']} + # for cookie in cookies] def close_driver(self): """关闭driver及浏览器""" diff --git a/DrissionPage/web_page.pyi b/DrissionPage/web_page.pyi index cf46938..ead925d 100644 --- a/DrissionPage/web_page.pyi +++ b/DrissionPage/web_page.pyi @@ -131,7 +131,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def get_tab(self, tab_id: str = None) -> WebPageTab: ... - def _get_driver_cookies(self, as_dict: bool = False, all_info: bool = False) -> dict: ... + # def _get_driver_cookies(self, as_dict: bool = False, all_info: bool = False) -> dict: ... def close_driver(self) -> None: ...