QWebEngineView - Javascript Callback

蓝咒 提交于 2019-12-23 03:44:26

问题


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.

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

回答1:


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


来源:https://stackoverflow.com/questions/45230931/qwebengineview-javascript-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!