Search code examples
pythoncookiespyqt5qtwebengineqwebengineview

How to disable cookies using qtwebengine?


PyQt5.14 QWebEngineView How do I disable cookies? I cannot find any reference to this.


Solution

  • One possible option is to use QWebEngineCookieStore associated with the profile to eliminate existing cookies and establish a filter to deny new cookies.

    from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        view = QtWebEngineWidgets.QWebEngineView()
        cookie_store = view.page().profile().cookieStore()
    
        def cookie_filter(request):
            print(
                f"firstPartyUrl: {request.firstPartyUrl.toString()}, origin: {request.origin.toString()}, thirdParty? {request.thirdParty}"
            )
            return False
    
        cookie_store.setCookieFilter(cookie_filter)
        cookie_store.deleteAllCookies()
        view.load(QtCore.QUrl("https://www.qt.io"))
        view.show()
        sys.exit(app.exec_())