Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QtWebEnginePage.runJavaScript
Qt 6.11 is out! See what's new in the release blog

QtWebEnginePage.runJavaScript

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 3 Posters 4.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rolln
    wrote on last edited by Rolln
    #1

    https://doc.qt.io/qtforpython/PySide2/QtWebEngineWidgets/QWebEnginePage.html#PySide2.QtWebEngineWidgets.PySide2.QtWebEngineWidgets.QWebEnginePage.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.

    R 1 Reply Last reply
    0
    • R Rolln

      https://doc.qt.io/qtforpython/PySide2/QtWebEngineWidgets/QWebEnginePage.html#PySide2.QtWebEngineWidgets.PySide2.QtWebEngineWidgets.QWebEnginePage.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.

      R Offline
      R Offline
      Rolln
      wrote on last edited by Rolln
      #2

      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()
      
      1 Reply Last reply
      0
      • S Offline
        S Offline
        skidooman
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          skidooman
          wrote on last edited by
          #4

          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.

          JonBJ 1 Reply Last reply
          0
          • S skidooman

            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.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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....

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved