Skip to main content
 首页 » 编程设计

python之无法使用 QWebEnginePage::runJavaScript() 更改 backgroundImage

2024年09月03日40TianFang

本来想解决this problem , 但正如我测试过 QWebEnginePage::runJavaScript() 的行为一样,我发现我什至无法使用带有以下代码的 QWebEnginePage::runJavaScript() 更改背景图像,为什么?

import sys 
 
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
from PyQt5.QtWidgets import * 
from PyQt5.QtWebEngineWidgets import * 
from PyQt5.QtWebEngineCore import * 
 
 
class WebEngineView(QWebEngineView): 
    def __init__(self, parent=None): 
        super().__init__(parent) 
 
        self.webPage = self.page() 
        # self.webPage = WebEnginePage() 
        self.webPage.load(QUrl('https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style')) 
        self.webPage.runJavaScript(''' 
                                window.addEventListener("load", function(event) { 
                                        alert(document.title); 
                                        document.body.style.backgroundImage = "url('https://www.w3schools.com/jsref/img_tree.png')"; 
 
                                }); 
                     ''')  # QWebEngineScript.MainWorld 
 
 
if __name__ == "__main__": 
 
    app = QApplication(sys.argv) 
    app.setAttribute(Qt.AA_UseSoftwareOpenGL) 
    webEngineView = WebEngineView() 
    webEngineView.show() 
    sys.exit(app.exec_()) 

请您参考如下方法:

您想要修改 DOM,因此它必须已经创建,在您的情况下,您在加载页面并且未构建 DOM 之前使用 runJavaScript。考虑到有 2 种可能的解决方案:

  • 使用loadFinished加载页面后发出执行脚本的信号:
import sys 
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets 
 
 
class WebEngineView(QtWebEngineWidgets.QWebEngineView): 
    def __init__(self, parent=None): 
        super().__init__(parent) 
        self.loadFinished.connect(self.on_load_finished) 
 
        self.load( 
            QtCore.QUrl( 
                "https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style" 
            ) 
        ) 
 
    @QtCore.pyqtSlot(bool) 
    def on_load_finished(self, ok): 
        if ok: 
            script = """ 
            alert(document.title); 
            document.body.style.backgroundImage = "url('https://www.w3schools.com/jsref/img_tree.png')"; 
            """ 
            self.page().runJavaScript(script) 
 
 
if __name__ == "__main__": 
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts) 
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_UseSoftwareOpenGL) 
    app = QtWidgets.QApplication(sys.argv) 
    w = WebEngineView() 
    w.show() 
    sys.exit(app.exec_()) 
import sys 
 
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets 
 
 
class WebEngineView(QtWebEngineWidgets.QWebEngineView): 
    def __init__(self, parent=None): 
        super().__init__(parent) 
 
        script = QtWebEngineWidgets.QWebEngineScript() 
        name = "test" 
        source = """ 
        alert(document.title); 
        document.body.style.backgroundImage = "url('https://www.w3schools.com/jsref/img_tree.png')"; 
        """ 
        script.setName(name) 
        script.setSourceCode(source) 
        script.setInjectionPoint( 
            QtWebEngineWidgets.QWebEngineScript.DocumentReady 
        ) 
        script.setRunsOnSubFrames(True) 
        script.setWorldId(QtWebEngineWidgets.QWebEngineScript.ApplicationWorld) 
        self.page().scripts().insert(script) 
 
        self.load( 
            QtCore.QUrl( 
                "https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style" 
            ) 
        ) 
 
 
if __name__ == "__main__": 
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts) 
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_UseSoftwareOpenGL) 
    app = QtWidgets.QApplication(sys.argv) 
    w = WebEngineView() 
    w.show() 
    sys.exit(app.exec_())