MCPcopy Create free account
hub / github.com/seleniumbase/SeleniumBase / save

Method save

seleniumbase/undetected/cdp_driver/browser.py:1162–1204  ·  view source on GitHub ↗

Save all cookies (or a subset, controlled by `pattern`) to a file to be restored later. :param file: :param pattern: regex style pattern string. any cookie that has a domain, key or value field which matches the pattern will be

(self, file: PathLike = ".session.dat", pattern: str = ".*")

Source from the content-addressed store, hash-verified

1160 await connection.send(cdp.network.set_cookies(cookies))
1161
1162 async def save(self, file: PathLike = ".session.dat", pattern: str = ".*"):
1163 """
1164 Save all cookies (or a subset, controlled by `pattern`)
1165 to a file to be restored later.
1166 :param file:
1167 :param pattern: regex style pattern string.
1168 any cookie that has a domain, key or value field
1169 which matches the pattern will be included.
1170 default = ".*" (all)
1171 Eg: the pattern "(cf|.com|nowsecure)" will include cookies which:
1172 - Have a string "cf" (cloudflare)
1173 - Have ".com" in them, in either domain, key or value field.
1174 - Contain "nowsecure"
1175 :type pattern: str
1176 """
1177 pattern = re.compile(pattern)
1178 save_path = pathlib.Path(file).resolve()
1179 connection = None
1180 for _tab in self._browser.tabs:
1181 if getattr(_tab, "closed", None):
1182 continue
1183 connection = _tab
1184 break
1185 else:
1186 connection = self._browser.connection
1187 cookies = await connection.send(cdp.network.get_cookies())
1188 # if not connection:
1189 # return
1190 # if not connection.websocket:
1191 # return
1192 # if connection.websocket.closed:
1193 # return
1194 cookies = await self.get_all(requests_cookie_format=False)
1195 included_cookies = []
1196 for cookie in cookies:
1197 for match in pattern.finditer(str(cookie.__dict__)):
1198 logger.debug(
1199 "Saved cookie for matching pattern '%s' => (%s: %s)"
1200 % (pattern.pattern, cookie.name, cookie.value)
1201 )
1202 included_cookies.append(cookie)
1203 break
1204 pickle.dump(cookies, save_path.open("w+b"))
1205
1206 async def load(self, file: PathLike = ".session.dat", pattern: str = ".*"):
1207 """

Callers 2

save_cookiesMethod · 0.80

Calls 4

get_allMethod · 0.95
get_cookiesMethod · 0.80
sendMethod · 0.45
openMethod · 0.45

Tested by

no test coverage detected