Python: Fill html form with PyQt and QtWebkit not displayed

1k views Asked by At

I am trying to fill a html form using python and PyQt and QtWebkit. I already found various posts on how to execute javascript and do something similar however my problem is I never get to see the results... the input field simply won't display what I set via javascript or other methods... Please check out that code below where I want to populate the google search field - I know I could initiate a google search directly via url but this is just an example, I need to get this working with javascript :/

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

app = QApplication(sys.argv)

web = QWebView()
web.load(QUrl("http://www.google.com"))

#web.page().mainFrame().evaluateJavaScript('var elem = document.getElementByID("lst-ib"); elem.value = "test";')

for e in web.page().mainFrame().findAllElements('input[id=lst-ib]').toList():
        e.setAttribute('value', 'test')

web.show()
app.exec_()
#sys.exit()

Could you help me out please?

Kind regards and thanks a lot :)

1

There are 1 answers

0
BPR On

Okay I found a way:

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



def executeJs():
    global web
    web.page().mainFrame().evaluateJavaScript("document.getElementsByName('q').item(0).value='test';")
    print "js executed"



app = QApplication(sys.argv)

web = QWebView()
web.load(QUrl("http://www.google.com"))
web.loadFinished.connect(executeJs)


web.show()
app.exec_()

Main problems were I guess that the evaluteJavaScript method had to be called after page is finished with loading and document.getElementById (after correcting the spelling error) did not work for some reason -> replaced with getElementsByName