PyQt4不会将我重定向到下一页 [英] PyQt4 does not redirect me to the next page
问题描述
下面的代码工作正常,除了一件事,它不遵循注册链接。
但是如果我转到我的实际浏览器和控制台类型:
The code below works fine except one thing, it does not follow the sign-up link. However if I go to my actual browser and in console type:
document.getElementById("link-signup").click()
它会将我重定向到所需的页面。我在想,问题积累,因为我没有启用某些功能的设置。但是,我不确定。
It will redirect me to the desired page. I was thinking that the problem accured because I didn't enable some feature in settings. but I'm not sure.
感谢任何帮助
#! /usr/bin/env python2.7
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import sys
class GrabberSettings(QWebPage):
def __init__(self):
QWebPage.__init__(self)
self.settings().setAttribute(QWebSettings.AutoLoadImages, False)
class Grabber(QWebView):
def __init__(self):
QWebView.__init__(self)
self.setPage(GrabberSettings())
self.loadFinished.connect(self._loadComplete)
self.doc = self.page().mainFrame().documentElement()
def _loadComplete(self):
print "Done"
link = self.doc.findFirst('a[link-signup]')
if link:
print "link found"
link.evaluateJavaScript('click()')
if __name__ == "__main__":
app = QApplication(sys.argv)
view = Grabber()
gmail = QUrl('https://accounts.google.com')
view.load(gmail)
view.show()
app.exec_()
推荐答案
我认为 click()
与google页面在加载后如何使用javascript来转换原始的 A
元素有关。如果您在 alert()
中包装 evaluateJavaScript()
调用,您可以看到点击
方法 null
I think the click()
failure may have something to do with how the google page uses javascript to transform the original A
element after it loads. If you wrap your evaluateJavaScript()
call in an alert()
, you can see that the click
method is null
link.evaluateJavaScript('this.click')
这不是100%的跨浏览器支持能够在链接上调用点击。
It is not a 100% cross-browser support to be able to call "click" on a link. It would need to be a button.
您有几个选择...
#1)导航到链接的href
def _loadComplete(self):
page = self.page()
doc = page.currentFrame().documentElement()
link = doc.findFirst('#link-signup')
if link and not link.isNull():
self.load(QUrl.fromEncoded(link.attribute('href').toAscii()))
(#2)模拟网络视图的点击
def _loadComplete(self):
page = self.page()
doc = page.currentFrame().documentElement()
link = doc.findFirst('#link-signup')
if link and not link.isNull():
pos = link.geometry().center()
self._doMouseClick(page, pos)
else:
print "Link not found"
@staticmethod
def _doMouseClick(obj, pos):
# mouse down
evt = QMouseEvent(QEvent.MouseButtonPress, pos,
Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(obj, evt)
# mouse up
evt = QMouseEvent(QEvent.MouseButtonRelease, pos,
Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(obj, evt)
$ b b
(#3)通过javascript使链接可点击
def _loadComplete(self):
page = self.page()
doc = page.currentFrame().documentElement()
link = doc.findFirst('#link-signup')
if link and not link.isNull():
link.evaluateJavaScript("""
var e = document.createEvent('MouseEvents');
e.initEvent('click', true, true);
this.dispatchEvent(e);
""")
这篇关于PyQt4不会将我重定向到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!