PyQt6 Setting and restoring the cache and cookies
Solved
QtWebEngine
-
Hi,
I have read the documentation but I am obviously missing something, hopefully not too obvious.
For the life of me, I cannot work out how to store the cache and cookies when using the QWebEngineView in PyQt6.
For reference please see the attached code.
As per the documentation:- I create a profile with a unique name,
- set the cache and storage paths,
- make sure the persistent storage policy is forced,
- make sure the HttpCacheType is DiskHttpCache.
- make sure I create the profile first before creating the page and engine view.
Additionally, I tried to called it "Default" without setting any paths, policies or cache type, as the docs say to do this in an attempt to get PyQt5 behaviour, but to no avail.
I'm really not sure where I'm going wrong.
Any help would be appreciated.################### def _getProfile(self) -> QWebEngineProfile: if self._profile is not None: self._printProfileDetails(self._profile) return self._profile if not (os.path.exists(self._CACHE_PATH) and os.path.isdir(self._CACHE_PATH)): os.makedirs(self._CACHE_PATH) if not (os.path.exists(self._STORAGE_PATH) and os.path.isdir(self._STORAGE_PATH)): os.makedirs(self._STORAGE_PATH) self._profile = QWebEngineProfile("MWVPersistentProfile") self._profile.setCachePath(self._CACHE_PATH) self._profile.setPersistentStoragePath(self._STORAGE_PATH) self._profile.setPersistentCookiesPolicy(QWebEngineProfile.PersistentCookiesPolicy.ForcePersistentCookies) self._profile.setHttpCacheType(QWebEngineProfile.HttpCacheType.DiskHttpCache) self._printProfileDetails(self._profile) return self._profile ################### def _generateWebEngineVew(self, parent = None) -> QWebEngineView: return QWebEngineView(QWebEnginePage(self._getProfile()), parent) ################### # Used for debugging def _printProfileDetails(self, profile: QWebEngineProfile): print("***********************") print(f"Storage Name: {profile.storageName()}") print(f"Cache Path: {profile.cachePath()}") print(f"Storage Path: {profile.persistentStoragePath()}") print(f"Cache Type: {profile.httpCacheType()}") print(f"Persistant Cookie Policy: {profile.persistentCookiesPolicy()}") print(f"Off The Record: {profile.isOffTheRecord()}") print("***********************")
Thanks in advanced,
John -
Updating the code to create the QWebEngineView to the following fixed the issue.
################### def _generateWebEngineVew(self, parent = None) -> QWebEngineView: webEngine: QWebEngineView = QWebEngineView(parent=parent) profile: QWebEngineProfile = self._getProfile() webPage: QWebEnginePage = QWebEnginePage(profile, webEngine) webEngine.setPage(webPage) return webEngine
-