gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:56:45 26 4
gpt4 key购买 nike

本来想解决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_())

关于python - 无法使用 QWebEnginePage::runJavaScript() 更改 backgroundImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56643674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com