微调cookies同步功能,待测试

This commit is contained in:
g1879 2023-03-24 17:52:34 +08:00
parent ea02a25c5d
commit ec42be7a2f
3 changed files with 10 additions and 9 deletions

View File

@ -287,18 +287,18 @@ class WebPageTab(SessionPage, ChromiumTab):
selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value'] selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
self.session.headers.update({"User-Agent": selenium_user_agent}) self.session.headers.update({"User-Agent": selenium_user_agent})
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, self._get_driver_cookies())
def cookies_to_browser(self): def cookies_to_browser(self):
"""把session对象的cookies复制到浏览器""" """把session对象的cookies复制到浏览器"""
if not self._has_driver: if not self._has_driver:
return return
set_browser_cookies(self, super().get_cookies(as_dict=True)) # set_browser_cookies(self, super().get_cookies(as_dict=True))
# set_browser_cookies(self, super().get_cookies(all_domains=True)) # set_browser_cookies(self, super().get_cookies(all_domains=True))
# set_browser_cookies(self, super().get_cookies()) set_browser_cookies(self, super().get_cookies())
def get_cookies(self, as_dict=False, all_domains=False, all_info=False): def get_cookies(self, as_dict=False, all_domains=False, all_info=False):
"""返回cookies """返回cookies

View File

@ -259,7 +259,8 @@ def set_browser_cookies(page, cookies):
if cookie.get('domain', None): if cookie.get('domain', None):
try: try:
page.run_cdp_loaded('Network.setCookie', **cookie) page.run_cdp_loaded('Network.setCookie', **cookie)
continue if is_cookie_in_driver(page, cookie):
continue
except Exception: except Exception:
pass pass

View File

@ -363,18 +363,18 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value'] selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
self.session.headers.update({"User-Agent": selenium_user_agent}) self.session.headers.update({"User-Agent": selenium_user_agent})
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, self._get_driver_cookies())
def cookies_to_browser(self): def cookies_to_browser(self):
"""把session对象的cookies复制到浏览器""" """把session对象的cookies复制到浏览器"""
if not self._has_driver: if not self._has_driver:
return return
set_browser_cookies(self, super().get_cookies(as_dict=True)) # set_browser_cookies(self, super().get_cookies(as_dict=True))
# set_browser_cookies(self, super().get_cookies(all_domains=True)) # set_browser_cookies(self, super().get_cookies(all_domains=True))
# set_browser_cookies(self, super().get_cookies()) set_browser_cookies(self, super().get_cookies())
def get_cookies(self, as_dict=False, all_domains=False, all_info=False): def get_cookies(self, as_dict=False, all_domains=False, all_info=False):
"""返回cookies """返回cookies