devmaster1
devmaster1

Reputation: 3

How to disable cookies using qtwebengine?

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

Upvotes: 0

Views: 685

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

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_())

Upvotes: 2

Related Questions