qt+evaluatejavascript

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')

It is not a 100% cross-browser support to be able to call "click" on a link. It would need to be a button.

You have a couple alternatives...

(#1) Just navigate to the href of the link

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) Simulate a click on the web view

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)

(#3) Make the link clickable via 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);  

""")

原文地址:http://stackoverflow.com/questions/13553817/pyqt4-does-not-redirect-me-to-the-next-page/13554601#13554601

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值