Qt - QWebView Problem

Posted by user547057 on Stack Overflow See other posts from Stack Overflow or by user547057
Published on 2010-12-24T19:42:29Z Indexed on 2010/12/24 19:54 UTC
Read the original article Hit count: 309

Filed under:
|
|
|

Hi,

I have a PyQt gui script which consists of a QWebView widget. I'm trying to send a GET request, i.e go to a page, fill a form and hit click using the code at the bottom of this question.

Since i'm dealing with the documentElement(a QWebElement) of the webview, I need to place all DOM actions in a separate function(which I have named fillForm) and connect the loadFinished() signal into the function. Without connecting the signal, the document will not have loaded and I won't be able to get the elements I want.

I'm able to submit the form correctly and get the proper response from the webpage.

The problem i'm having is that, the above leads to a sort of infinite loop. This is because the webpage gets reloaded each time a new page is loaded, so the form gets filled each and every single time without stopping.

I'd like to know if there's some way of finding out whether the WebView's page has loaded fully, non-asynchronously or maybe pause execution of the script(without freezing the gui) until the whole document has loaded. I'm unable to come up with a satisfactory solution(my idea consisted of keeping a global variable to track clicks) to this problem. I would appreciate it if someone could help me out with a better way to tackle this. Thanks!

Here's the code i'm using

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4 import QtCore

app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://mywebsite.com"))

def fillForm():
    doc = web.page().mainFrame().documentElement()
    searchform = doc.findFirst("input[type=text]")
    searchform.setAttribute("value", "hello")
    button = doc.findFirst("input[type=submit]")
    button.evaluateJavaScript("click()")

QtCore.QObject.connect(web, QtCore.SIGNAL("loadFinished"), fillForm)
web.show()
sys.exit(app.exec_())

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt