QWebEngineView - Javascript 回调 [英] QWebEngineView - Javascript Callback

查看:167
本文介绍了QWebEngineView - Javascript 回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最终想要完成的是捕获用户进入网站的用户名和密码.例如,如果用户在 Facebook 登录中输入test@example.com"作为电子邮件地址,然后单击提交,我想将该电子邮件地址存储在我的 PyQt 应用程序中.

What I am ultimately trying to accomplish is to capture the username and password that the user enters into a website. For example, if the user enters "test@example.com" as the email address into Facebook's login, then clicks submit, I want to store that email address in my PyQt Application.

我最接近实现这一点的方法是使用一系列 JavaScript 命令在登录按钮"上放置一个侦听器,该侦听器返回用户参数的当前值.我的问题是 PyQt 提供的回调是用于 runJavaScript 函数完成时的,而不是 javascript 事件侦听器.我想知道是否有任何方法可以从 JavaScript 函数中捕获回调函数,或者是否有更好的方法来执行此操作.

The closest I've come to achieving this has been using a series of JavaScript commands to place a listener on the "Login Button" that returns the current value of the user parameter. My problem is that the callback that PyQt provides is for when the runJavaScript function is completed, not the javascript event listener. I'm wondering if there is any way to capture the callback function from the JavaScript function, or if there is a better way altogether for me to do this.

import os
import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtCore import QUrl, QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView

class WebPage(QWebEngineView):
    def __init__(self):
        QWebEngineView.__init__(self)
        self.load(QUrl("https://facebook.com"))
        self.loadFinished.connect(self._on_load_finished)
        #self.page().runJavaScript("document.getElementById("myBtn").addEventListener("click", displayDate)", print)

    def _on_load_finished(self):
        print("Finished Loading")
        cmds = ["btn=document.getElementById('u_0_r')",  # Login Button
                "user=document.getElementsByName('email')[0]",
                "function get_username(){return user.value}",
                "btn.addEventListener('click', get_username)"]
        self.page().runJavaScript("; ".join(cmds), lambda x: print("test: %s"  % x))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    web = WebPage()
    web.show()
    sys.exit(app.exec_())  # only need one app, one running event loop

推荐答案

我找到了一种使用urlChanged"信号的解决方法,目前看来对我的应用程序有效

I found a work around using the "urlChanged" signal that seems to work so far for my applications

import os
import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtCore import QUrl, QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor

class WebPage(QWebEngineView):
    def __init__(self):
        QWebEngineView.__init__(self)
        self.current_url = ''
        self.load(QUrl("https://facebook.com"))
        self.loadFinished.connect(self._on_load_finished)
        self.urlChanged.connect(self._on_url_change)

    def _on_load_finished(self):
        self.current_url = self.url().toString()

    def _on_url_change(self):
        self.page().runJavaScript("document.getElementsByName('email')[0].value", self.store_value)

    def store_value(self, param):
        self.value = param
        print("Param: " +str(param))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    web = WebPage()
    web.show()
    sys.exit(app.exec_())  # only need one app, one running event loop

这篇关于QWebEngineView - Javascript 回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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