I am trying to get some data from QWebEngineView using runJavaScript function but it errors out showing the below error message.
Is there a way to solve this? Older topics suggest this is a limitation in Pyside2, so not sure if it's addressed by now.
from PySide2 import QtCore, QtWidgets, QtGui, QtWebEngineWidgets
def callbackfunction(html):
print html
file = "myhtmlfile.html"
view = QtWebEngineWidgets.QWebEngineView()
view.load(QtCore.QUrl.fromLocalFile(file))
view.page().runJavaScript("document.getElementsByTagName('html')[0].innerHTML", callbackfunction)
TypeError: 'PySide2.QtWebEngineWidgets.QWebEnginePage.runJavaScript' called with wrong argument types:
PySide2.QtWebEngineWidgets.QWebEnginePage.runJavaScript(str, function)
Supported signatures:
PySide2.QtWebEngineWidgets.QWebEnginePage.runJavaScript(str)
PySide2.QtWebEngineWidgets.QWebEnginePage.runJavaScript(str, int)
PySide2 does not provide all of the overload methods of runJavaScript so it does not support passing a callback to it. A possible workaround is to use QtWebChannel that through websockets implements the communication between javascript and python:
My previous logic focuses only on obtaining the HTML but in this part of the answer I will try to generalize the logic to be able to associate callbacks. The idea is to send the response to the bridge object associating a uuid that is related to the callback, the message must be sent in json format to be able to handle different types of data.