0

I using pyqt5 for taking the screenshot to the URL. it's the react component. it takes the screenshot only component. don't wait till the API response. wanna take a screenshot after get the API response.

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt, QUrl, QTimer
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings


class Screenshot(QWebEngineView):

    def capture(self, url, output_file):
        self.output_file = output_file
        self.load(QUrl(url))
        self.loadFinished.connect(self.on_loaded)
        self.setAttribute(Qt.WA_DontShowOnScreen)
        self.show()

    def on_loaded(self):
        size = self.page().contentsSize().toSize()
        self.resize(size)
        qt = QTimer()
        qt.setInterval(8000)
        qt.setSingleShot(True)
        qt.singleShot(8000, self.take_screenshot)

    def take_screenshot(self):
        self.grab().save(self.output_file, b'PNG')
        self.app.quit()



app = QApplication([])
s = Screenshot()
s.app = app
s.capture('https://doc.qt.io/qt-5/qtimer.html', 'webpage20.png')
app.exec_()

Note: Under s.capture param URL is just for the example purpose.

Ankit Kumar Rathod
  • 503
  • 1
  • 4
  • 7
  • The `loadFinished` signal only notifies that the *loading* of the page has been completed, not that it has been rendered. Also, since you're not showing the widget by using the `WA_DontShowOnScreen` attribute, it cannot be rendered, and you wouldn't grab the page anyway, since `grab()` only grabs the *window* (which is not rendered, by the way, due to the attribute above). Finally, `QTimer.singleShot` is a static, you don't need to create a new instance to do that, it should just be `QTimer.singleShot(8000, self.take_screenshot)`. – musicamante Dec 01 '20 at 16:43
  • I don't understand your question, I have the following with your code: https://i.imgur.com/dcmbt85.png – eyllanesc Dec 01 '20 at 17:34
  • My question is when I take the screenshot from the URL, it takes the screenshot without waiting for the load content from the API response. @eyllanesc – Ankit Kumar Rathod Dec 02 '20 at 05:39

0 Answers0