修复WebPage的get_cookies()方法不能获取所有域名的问题

This commit is contained in:
g1879 2023-06-25 07:31:24 +08:00
parent b47a903ae1
commit a0c5072b12
4 changed files with 37 additions and 34 deletions

View File

@ -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(as_dict=True))
# set_session_cookies(self.session, self._get_driver_cookies(all_domains=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): def cookies_to_browser(self):
"""把session对象的cookies复制到浏览器""" """把session对象的cookies复制到浏览器"""
@ -320,22 +320,23 @@ class WebPageTab(SessionPage, ChromiumTab):
if self._mode == 's': if self._mode == 's':
return super().get_cookies(as_dict, all_domains, all_info) return super().get_cookies(as_dict, all_domains, all_info)
elif self._mode == 'd': 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): # def _get_driver_cookies(self, as_dict=False, all_info=False):
"""获取浏览器cookies # """获取浏览器cookies
:param as_dict: 是否以dict形式返回为True时all_info无效 # :param as_dict: 是否以dict形式返回为True时all_info无效
:param all_info: 是否返回所有信息为False时只返回namevaluedomain # :param all_info: 是否返回所有信息为False时只返回name、value、domain
:return: cookies信息 # :return: cookies信息
""" # """
cookies = self.run_cdp('Network.getCookies')['cookies'] # cookies = self.run_cdp('Network.getCookies')['cookies']
if as_dict: # if as_dict:
return {cookie['name']: cookie['value'] for cookie in cookies} # return {cookie['name']: cookie['value'] for cookie in cookies}
elif all_info: # elif all_info:
return cookies # return cookies
else: # else:
return [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']} # return [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']}
for cookie in cookies] # for cookie in cookies]
def _find_elements(self, loc_or_ele, timeout=None, single=True, relative=False, raise_err=None): def _find_elements(self, loc_or_ele, timeout=None, single=True, relative=False, raise_err=None):
"""返回页面中符合条件的元素、属性或节点文本,默认返回第一个 """返回页面中符合条件的元素、属性或节点文本,默认返回第一个

View File

@ -124,7 +124,7 @@ class WebPageTab(SessionPage, ChromiumTab):
def get_cookies(self, as_dict: bool = False, all_domains: bool = False, def get_cookies(self, as_dict: bool = False, all_domains: bool = False,
all_info: bool = False) -> Union[dict, list]: ... 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的函数----------------------- # ----------------重写SessionPage的函数-----------------------
def post(self, def post(self,

View File

@ -350,7 +350,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value'] user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
self.session.headers.update({"User-Agent": user_agent}) 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): def cookies_to_browser(self):
"""把session对象的cookies复制到浏览器""" """把session对象的cookies复制到浏览器"""
@ -371,7 +372,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
if self._mode == 's': if self._mode == 's':
return super().get_cookies(as_dict, all_domains, all_info) return super().get_cookies(as_dict, all_domains, all_info)
elif self._mode == 'd': 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): def get_tab(self, tab_id=None):
"""获取一个标签页对象 """获取一个标签页对象
@ -381,20 +383,20 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
tab_id = tab_id or self.tab_id tab_id = tab_id or self.tab_id
return WebPageTab(self, tab_id) return WebPageTab(self, tab_id)
def _get_driver_cookies(self, as_dict=False, all_info=False): # def _get_driver_cookies(self, as_dict=False, all_info=False):
"""获取浏览器cookies # """获取浏览器cookies
:param as_dict: 是否以dict形式返回为True时all_info无效 # :param as_dict: 是否以dict形式返回为True时all_info无效
:param all_info: 是否返回所有信息 # :param all_info: 是否返回所有信息
:return: cookies信息 # :return: cookies信息
""" # """
cookies = self.run_cdp('Network.getCookies')['cookies'] # cookies = self.run_cdp('Network.getCookies')['cookies']
if as_dict: # if as_dict:
return {cookie['name']: cookie['value'] for cookie in cookies} # return {cookie['name']: cookie['value'] for cookie in cookies}
elif all_info: # elif all_info:
return cookies # return cookies
else: # else:
return [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']} # return [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']}
for cookie in cookies] # for cookie in cookies]
def close_driver(self): def close_driver(self):
"""关闭driver及浏览器""" """关闭driver及浏览器"""

View File

@ -131,7 +131,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
def get_tab(self, tab_id: str = None) -> WebPageTab: ... 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: ... def close_driver(self) -> None: ...