getMail is executed on the browser, however according to the doc's, to get the result, I would need to pass in a callback function as a second argument, however doing so yields a:TypeError: `runJavaScript() takes exactly one argument (2 given) #推荐答案一个可能的解决方案是注入一个 QObject,它允许使用 Qt WebChannel 与 DOM 通信:A possible solution is to inject a QObject that allows to communicate with the DOM using Qt WebChannel:import osimport sysfrom PySide2 import QtCore, QtWidgets, QtWebEngineWidgets, QtWebChannelfrom jinja2 import Templateclass Element(QtCore.QObject): loaded = QtCore.Signal() def __init__(self, name, parent=None): super(Element, self).__init__(parent) self._name = name self._is_loaded = False @property def name(self): return self._name @property def is_loaded(self): return self._is_loaded @QtCore.Slot() def set_loaded(self): self._is_loaded = True self.loaded.emit() def render_script(self, script, **kwargs): kwargs["name"] = self.name return Template(script).render(**kwargs)class TestObject(Element): @QtCore.Slot(str) def test(self, res): print(res)class WebEnginePage(QtWebEngineWidgets.QWebEnginePage): def __init__(self, *args, **kwargs): super(WebEnginePage, self).__init__(*args, **kwargs) self.loadFinished.connect(self.onLoadFinished) self._objects = [] def add_object(self, obj): self._objects.append(obj) @QtCore.Slot(bool) def onLoadFinished(self, ok): if ok: self.load_qwebchannel() self.load_objects() def load_qwebchannel(self): file = QtCore.QFile(":/qtwebchannel/qwebchannel.js") if file.open(QtCore.QIODevice.ReadOnly): content = file.readAll() file.close() self.runJavaScript(content.data().decode()) if self.webChannel() is None: channel = QtWebChannel.QWebChannel(self) self.setWebChannel(channel) def load_objects(self): if self.webChannel() is not None: objects = {obj.name: obj for obj in self._objects} self.webChannel().registerObjects(objects) _script = r""" {% for obj in objects %} var {{obj}} = null; {% endfor %} new QWebChannel(qt.webChannelTransport, function (channel) { {% for obj in objects %} {{obj}} = channel.objects.{{obj}}; {{obj}}.set_loaded() {% endfor %} }); """ self.runJavaScript(Template(_script).render(objects=objects.keys()))class WebPage(QtWebEngineWidgets.QWebEngineView): def __init__(self, parent=None): super().__init__(parent) page = WebEnginePage(self) self.setPage(page) self.test_object = TestObject("test_object", self) self.test_object.loaded.connect(self.test_object_loaded) page.add_object(self.test_object) self.load(QtCore.QUrl("https://mytestpage.com")) @QtCore.Slot() def test_object_loaded(self): script = self.test_object.render_script( r""" {{name}}.test(window.getMail()); """ ) self.page().runJavaScript(script)if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) web = WebPage() web.show() sys.exit(app.exec_()) 这篇关于如何从 PyQt 的 QWebEnginePage.runJavaScript() 获取返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-26 15:14