PyQt5: mouseClick and source-code in QWebEngineView
<p>I have a working script that uses PyQt-5.5.1, which I now want to port to a new PyQt version (5.7). Adapting most of the things was fine, but I faced two major problems: (1) to perform a (simulated) mouseclick, (2) to access (let's say: print) the html source-code of a webpage which is currently displayed in the QWebView or QWebEngineView, respectively.</p>

<p>For example, I could do the following using QWebView in PyQt-5.5.1:</p>

<pre><code>QTest.mouseClick(self.wvTest, Qt.LeftButton, QPoint(x, y))
</code></pre>

<p>and</p>

<pre><code>frame = self.wvTest.page().mainFrame()
print(frame.toHtml().encode('utf-8'))
</code></pre>

<p>I am aware of the <a href="http://pyqt.sourceforge.net/Docs/PyQt5/api/qwebengineview.html" rel="nofollow noreferrer">docs</a> as well as <a href="https://wiki.qt.io/Porting_from_QtWebKit_to_QtWebEngine" rel="nofollow noreferrer">this page</a> about porting to QWebEngineView but unable to convert C++ notation to a working Python code.</p>

<p>How can I adapt this to QWebEngineView in PyQt-5.7? Below is a fully working snippet for PyQt-5.5.1, which fails for the new PyQt-version:</p>

<ul>
<li>for Button1: no mouse click reaction at all.</li>
<li>for Button2: <code>AttributeError: 'QWebEnginePage' object has no attribute 'mainFrame'</code>, and when I delete the mainframe(): <code>TypeError: toHtml(self, Callable[..., None]): not enough arguments</code>.</li>
</ul>

<p><pre>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</p>

<p>if PYQT_VERSION_STR=='5.5.1': from PyQt5 import QtWebKitWidgets
else: from PyQt5 import QtWebEngineWidgets</p>

<code>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_())
</code></pre>

<p></p>