为什么 QWebView.loadFinished 在某些站点上被多次调用,例如YouTube? [英] Why is QWebView.loadFinished called several times on some sites e.g. youtube?

查看:29
本文介绍了为什么 QWebView.loadFinished 在某些站点上被多次调用,例如YouTube?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,loadFinished 应仅在所有页面元素加载完成后才发出.这应该意味着它只会被调用一次,但是我注意到在 youtube.com 等某些网站上,它会被调用两次?有没有其他方法可以解决这个bug 或者检测 page.load 事件的最可靠方法是什么?

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 ?

测试代码如下:

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

输出:

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

有一个几乎相同的问题,但它已经超过 2 年了,仍未得到答复.

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

推荐答案

load* 信号在每帧加载时触发一次.

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)

您可以通过连接到 frameCreated 信号,对于主框架加载后创建的每个后续帧都会触发一次:

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)

这篇关于为什么 QWebView.loadFinished 在某些站点上被多次调用,例如YouTube?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆