Search code examples
pythonpyqt5pyside6

Do multiple QWebEngineViews use the same web page cache profile


I have multiple instances of QWebEngineView, each of which loads the same URL (e.g. "https://*****.com/login),

widget = Qwidget()
lay= QHBoxLayout()
initialUrl = "https://******.com/login/"
for i in range(3):
    frame = QFrame()
    web_lay = QVBoxlayout()
    webView = QWebEngineView()
    webView.load(QUrl(initialUrl))

    reload_button = QPushButton(QIcon(":/icons/images/icons/cil-loop"), "", self)
    reload_button.setToolTip("reload")
    reload_button.clicked.connect(lambda: webView.reload()) 
   # This is a wrong way of writing, so each button cannot be corresponding to WebView.
   # I write this just to express that I have a corresponding button for each

    web_lay.addwidget(webView)
    web_lay.addWidget(reload_button)
    Frame.setLayout(web_lay)
    
    lay.addwidget(frame)

Once I login to the corresponding website account in a QWebEngineView, the page displayed will also be refreshed after other pages are refreshed,refresh(clicked reload button) the interface in the unlogin QWebEngineView, and the refreshed interface will be the logged interface.

What I excepted is that each QWebEngineView can log in to a different account

Anyone else faced such issue?


Solution

  • The issue is that each QWebEngineView uses the same default QWebEngineProfile unless specified otherwise. What you need to do is create a unique QWebEngineProfile for each QWebEngineView and create a QWebEnginePage that uses that profile and assign it to the QWebEngineView...

    For example:

    from PySide6.QtWebEngineCore import QWebEngineProfile, QWebEnginePage
    
    ...
    for i in range(3):
        storage = "unique/storage/location" + str(i) # <- unique for each iteration
        frame = QFrame()
        web_lay = QVBoxlayout()
        webView = QWebEngineView()
        profile = QWebEngineProfile(storage, webview)
        page = QWebEnginePage(profile, webview)
        webview.setPage(page)
        webView.load(QUrl(initialUrl))
        ...
    
    ...
    

    Once you have assigned the page to the view each view will run independently of the other. The will each have a separate cache, separate histories, separate cookies, separate settings etc.