[英]Python: Fill html form with PyQt and QtWebkit not displayed
I am trying to fill a html form using python and PyQt and QtWebkit. 我正在尝试使用python和PyQt和QtWebkit填写html表单。 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 :/
我已经找到了各种有关如何执行javascript并执行类似操作的文章,但是我的问题是我永远都看不到结果...输入字段根本不会显示我通过javascript或其他方法设置的内容...请签出我想在其中填充Google搜索字段的以下代码-我知道我可以直接通过url发起Google搜索,但这只是一个示例,我需要使用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 :) 亲切的问候,非常感谢:)
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.getElementByI d (after correcting the spelling error) did not work for some reason -> replaced with getElementsByName
主要问题是我猜想页面加载完成后必须调用evaluteJavaScript方法,并且由于某种原因document.getElementByI d (在纠正了拼写错误之后)不起作用->替换为getElementsByName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.