使用PyQt和QWebview填写表单

use*_*778 8 python screen-scraping pyqt qtwebkit qwebview

我想使用PyQt/QWebview来1)加载特定的URL,2)将信息输入表单,3)单击按钮/链接.Mechanize不起作用,因为我需要一个实际的浏览器.

这是我的代码:

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@email.com")
    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_())
Run Code Online (Sandbox Code Playgroud)

页面正确加载,但未输入任何输入,并且未提交表单.有任何想法吗?

Den*_*kov 7

这有助于我使它工作:

user.setAttribute("value", "email@email.com")
-->
user.evaluateJavaScript("this.value = 'email@email.com'")
Run Code Online (Sandbox Code Playgroud)

属性和属性是不同的东西.

还有一个问题:

click() --> this.click()
Run Code Online (Sandbox Code Playgroud)


Bre*_*ell 0

您也许可以使用 Webkit/QWebView 来做到这一点,但是使用 selenium 怎么样: http: //code.google.com/p/selenium/?它正是为这种浏览器自动化而设计的,并且具有很好的 python 绑定。