QWebView如何模拟用户点击
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 15 18:17:47 2013
@author: Administrator
"""
#!/usr/bin/env python
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *
import time
class DlgWebTest(QDialog):
def __init__(self, *args, **argvs ):
super(DlgWebTest, self).__init__(*args, **argvs)
self.webView = QWebView(self)
self.webView.loadFinished.connect(self.finishLoading)
self.showMaximized()
self.webView.showMaximized()
self.first_load = True
def setUrl(self, url):
self.webView.setUrl(url)
def finishLoading(self):
# Begin document inspection.
print('finisthloading')
frame = self.webView.page().mainFrame()
document = frame.documentElement()
time.sleep(3)
self.next_page(document)
# self.parse_taobao(document)
# self.test(document)
# print(frame.url())
def next_page(self,document):
btn_next = document.findFirst('a[class="page-next"]')
print('review',btn_next)
print(btn_next.document())
print(btn_next.toPlainText())
btn_next.evaluateJavaScript("this.click()")
def parse_taobao(self,document):
review = document.findFirst('a[data-index="1"]')
print('review',review)
print(review.document())
review.evaluateJavaScript("this.click()")
print(review.toPlainText())
print(review.tagName())
review.evaluateJavaScript("click()")
# document.evaluateJavaScript("document.getAttribute('className').click()")
def test(self,document):
z = document.findFirst('input[id="kw"]')
z.setAttribute('value',u'我不知道')
btn = document.findFirst('input[id="su"]')
btn.evaluateJavaScript("this.click()")
# document.evaluateJavaScript("document.getElementById('su').click()")
def examineChildElements(self, parentElement):
# Traverse the document.
element = parentElement.firstChild()
while not element.isNull():
self.examineChildElements(element)
element = element.nextSibling()
# print(element.attribute())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
url = 'http://item.taobao.com/item.htm?id=19655021071#reviews'
# baidu = 'http://www.baidu.com'
window = DlgWebTest()
window.show()
window.setUrl(QtCore.QUrl(url))
sys.exit(app.exec_())
[解决办法]
QWebElement btn = document.findFirst("input[type=submit]");
btn.evaluateJavaScript("this.click()");