PyQt5:QWebEngineView中的mouseClick和源代码

nos*_*mus 5 python qtwebkit qtestlib pyqt5 qtwebengine

我有一个使用PyQt-5.5.1的工作脚本,我现在想要移植到一个新的PyQt版本(5.7).适应大多数事情很好,但我面临两个主要问题:(1)执行(模拟)鼠标点击,(2)访问(假设:打印)当前显示的网页的html源代码分别是QWebView或QWebEngineView.

例如,我可以使用PyQt-5.5.1中的QWebView执行以下操作:

QTest.mouseClick(self.wvTest, Qt.LeftButton, QPoint(x, y))
Run Code Online (Sandbox Code Playgroud)

frame = self.wvTest.page().mainFrame()
print(frame.toHtml().encode('utf-8'))
Run Code Online (Sandbox Code Playgroud)

我知道有关移植到QWebEngineView但无法将C++表示法转换为可用的Python代码的文档以及此页面.

如何在PyQt-5.7中将其改编为QWebEngineView?下面是PyQt-5.5.1的一个完整工作片段,它对于新的PyQt版本失败了:

  • 对于Button1:根本没有鼠标点击反应.
  • 对于Button2:,AttributeError: 'QWebEnginePage' object has no attribute 'mainFrame'当我删除大型机()时:TypeError: toHtml(self, Callable[..., None]): not enough arguments.

import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
from PyQt5.QtCore import QRect, Qt, QUrl, QPoint, QEvent
from PyQt5.QtTest import QTest
from PyQt5.Qt import PYQT_VERSION_STR

if PYQT_VERSION_STR=='5.5.1': from PyQt5 import QtWebKitWidgets else: from PyQt5 import QtWebEngineWidgets

class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.button1 = QPushButton('Button1', self) self.button1.clicked.connect(self.buttonOne) self.button1.setGeometry(QRect(10, 10, 90, 20)) self.button2 = QPushButton('Button2', self) self.button2.clicked.connect(self.buttonTwo) self.button2.setGeometry(QRect(110, 10, 90, 20)) if PYQT_VERSION_STR=='5.5.1': self.wvTest = QtWebKitWidgets.QWebView(self) else: self.wvTest = QtWebEngineWidgets.QWebEngineView(self) self.wvTest.setGeometry(QRect(10, 40, 430, 550)) self.wvTest.setUrl(QUrl('http://www.startpage.com')) self.wvTest.setObjectName('wvTest') self.setGeometry(300, 300, 450, 600) self.setWindowTitle('WebView minimalistic') self.show() def buttonOne(self): qp = QPoint(38, 314) QTest.mouseClick(self.wvTest, Qt.LeftButton, pos=qp) # or: QTest.mouseMove(self.wvTest, pos=self.qp) print('Button1 pressed.') def buttonTwo(self): frame = self.wvTest.page().mainFrame() print(frame.toHtml().encode('utf-8')) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

ekh*_*oro 7

QWebEngineView课程不是替代课程QWebView.正如移植指南所阐明的那样,许多API已经发生了根本性的变化,并且一些主要功能完全缺失.除非您的浏览器实现非常非常简单,否则这可能会导致无法编写兼容层.编写单独的测试套件很可能更容易(除非你不介意编写大量的条件代码).

首先,您需要为QTBUG-43602实施解决方案.当前的设计QWebEngineView意味着内部QOpenGLWidget处理鼠标事件,因此每当加载页面时,您的代码都需要获得对它的新引用:

class Example(QWidget):
    ...

    def initUI(self):
        ...    
        self._glwidget = None

        if PYQT_VERSION_STR=='5.5.1':
            self.wvTest = QtWebKitWidgets.QWebView(self)
        else:
            self.wvTest = QtWebEngineWidgets.QWebEngineView(self)
            self.wvTest.installEventFilter(self)
        ...

    def eventFilter(self, source, event):
        if (event.type() == QEvent.ChildAdded and
            source is self.wvTest and
            event.child().isWidgetType()):
            self._glwidget = event.child()
            self._glwidget.installEventFilter(self)
        elif (event.type() == QEvent.MouseButtonPress and
              source is self._glwidget):
            print('web-view mouse-press:', event.pos())
        return super().eventFilter(source, event)

    def buttonOne(self):
        qp = QPoint(38, 314)
        widget = self._glwidget or self.wvTest
        QTest.mouseClick(widget, Qt.LeftButton, pos=qp)
Run Code Online (Sandbox Code Playgroud)

要访问页面的html,您需要一些条件代码,因为Web引擎API异步工作,需要回调.此外,没有用于处理Web引擎中的帧的内置API(您需要使用javascript),因此所有内容都需要通过网页:

    def buttonTwo(self):
        if PYQT_VERSION_STR=='5.5.1':
            print(self.wvTest.page().toHtml())
        else:
            self.wvTest.page().toHtml(self.processHtml)

    def processHtml(self, html):
        print(html)
Run Code Online (Sandbox Code Playgroud)