Why is QWebView.loadFinished called several times on some sites e.g. youtube?

荒凉一梦 提交于 2019-12-30 03:26:11

问题


As per the documentation, loadFinished should be emitted only after all the page elements have finished loading. This should mean that it'll be called only once, however I've noticed that on some sites like youtube.com, it gets called twice ? Is there any other way to get around this bug or whats the most reliable way to detect page.load event ?

Here's the test code :

import sys
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtCore import QUrl
from PyQt4.QtGui import QApplication

def onDone(val):
    print "Done ...",val

def onStart():
    print "Started..."   

app = QApplication(sys.argv)
ui =  QtWebKit.QWebView()
ui.loadStarted.connect(onStart)
ui.loadFinished.connect(onDone)

ui.load(QUrl("http://www.youtube.com"))   
ui.showMaximized()
sys.exit(app.exec_())

The output:

Started...
Done ... True
Started...
Done ... True

Edit: There's an almost same question but its > 2yrs old and still unanswered.


回答1:


The load* signals are fired once for each frame that is loaded.

To capture only the first set of signals, connect to the corresponding signals of the main frame:

ui.page().mainFrame().loadStarted.connect(onStart)
ui.page().mainFrame().loadFinished.connect(onDone)

You can verify that other frames are being loaded by connecting to the frameCreated signal, which will fire once for each subsequent frame created after the main frame has loaded:

def onFrame(val):
    print 'Frame Created:', val.frameName()

ui.page().frameCreated.connect(onFrame)



回答2:


I am quite sure that happens when you modify the DOM like if it was a string. I am sure QWebKit runs JS code, so it could modify the DOM after it was onDone().

You should probably create an onDoneFirst method that only fires once.



来源:https://stackoverflow.com/questions/8775152/why-is-qwebview-loadfinished-called-several-times-on-some-sites-e-g-youtube

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!