使用 PyQt 和 QWebview 填写表单 [英] Filling out a form using PyQt and QWebview

查看:87
本文介绍了使用 PyQt 和 QWebview 填写表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的代码:

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

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

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

推荐答案

这帮助我使它工作:

user.setAttribute("value", "email@email.com")
-->
user.evaluateJavaScript("this.value = 'email@email.com'")

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

Attribute and property are different things.

另外一个修复:

click() --> this.click()

这篇关于使用 PyQt 和 QWebview 填写表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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