QtWebEnginePage.runJavaScript
-
In the C++ class you can use a callback to get the value returned from the javascript but it doesn't appear that you can get a return value/object with PySide2. Any suggestions would be great.
-
Here is a basic example...
When using QT, temp = None.
When the same script is run using Selenium, temp = 5.import sys import time from PySide2 import QtCore, QtGui, QtWidgets from PySide2 import QtWebEngineWidgets from PySide2.QtWebEngineWidgets import QWebEngineView app = QtWidgets.QApplication(sys.argv) view = QtWebEngineWidgets.QWebEngineView() view.setUrl(QtCore.QUrl("https://google.com")) view.show() temp = view.page().runJavaScript('return 1+4') print(temp)
import sys import time from selenium import webdriver browser_options = webdriver.FirefoxOptions() browser_options.add_argument('-headless') browser = webdriver.Firefox(firefox_options=browser_options) browser.get('https://google.com') temp = browser.execute_script('return 1+4') print(temp) browser.close() browser.quit()
-
I found the answer here:
https://stackoverflow.com/questions/41780435/return-value-from-javascript-to-pyqt5
I faced the same issue, but in PySide. Back in PySide (1) I could do this:
self.page().mainFrame().evaluateJavaScript("getHits()")
but now, there is no more evaluateJavaScript() but a runJavaScript. This function can take only one or two params. Since it is asynchronous, you apparently have to pass a function as the second parameter. When the script finishes execution, it will call that second function and you will then get the data.
-
Argh... seems PYQT5 can do this, but not PySide2.
https://bugreports.qt.io/browse/PYSIDE-643
Too bad since doing this was easy in PySide.
-
@skidooman
We convered this recently in https://forum.qt.io/topic/110659/q_arg-missing-in-python-how-to-use-invokemethod.
A couple of posts there claim workarounds, I didn't look at them but you might want to....