Search code examples
qtpyqtqwebview

Different QWebView objects produce identical output for same urls


I found that if we load one url with different QWebView objects parallely, we will get identical output: only one of this QWebView will make real request. Here is example:

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebKitWidgets import QWebView

def load_and_print(url):
    view = QWebView()
    view.load(QUrl(url))
    def on_ready(ok):
        print(view.page().mainFrame().documentElement().toPlainText())
    view.loadFinished.connect(on_ready)

if __name__ == '__main__':
    app = QApplication([])

    # This url outputs random number:
    url = 'https://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new'
    load_and_print(url)
    load_and_print(url)

    app.exec_()

    # output:
    # 39
    # 39

How can we get "fair", unique output for each QWebView object, If url is the same?

PyQt 5.4, windows 7


Solution

  • Solution is to disable cache:

    view.page().settings().setObjectCacheCapacities(0, 0, 0)