Filling out a form using PyQt and QWebview

user1137778 picture user1137778 · Jun 16, 2012 · Viewed 8.5k times · Source

I would like to use PyQt/QWebview to 1) load a specific url, 2) enter information into a form, 3) click buttons/links. Mechanize does not work because I need an actual browser.

Here's my code:

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("https://www.lendingclub.com/account/gotoLogin.action"))

def fillForm():
    doc = web.page().mainFrame().documentElement()
    user = doc.findFirst("input[id=master_username]")
    passwd = doc.findFirst("input[id=master_password]")

    user.setAttribute("value", "[email protected]")
    passwd.setAttribute("value", "password")


    button = doc.findFirst("input[id=master_sign-in-submit]")
    button.evaluateJavaScript("click()")

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

The page loads correctly, but no input is entered and the form is not submitted. Any ideas?

Answer

Denis Ryzhkov picture Denis Ryzhkov · Jul 5, 2012

This helped me to make it work:

user.setAttribute("value", "[email protected]")
-->
user.evaluateJavaScript("this.value = '[email protected]'")

Attribute and property are different things.

One more fix:

click() --> this.click()