Wait until you capture a capture image with PyQt

I work with PyQt (as a Python beginner). I need to take screenshots of a website in a headless system. I used to use PhantomJS for another project, but they refused to support Flash in 1.5, and I do not want to rely on the outdated version 1.4 for my new project.

So, I use PyQt to do my own things. I can take a screenshot from the site with the given URL, no problem.

But I still have the plugin icon for the β€œblue dice” on the flash placeholder (yes, javascript and plugins are activated)

self.settings.setAttribute(QtWebKit.QWebSettings.PluginsEnabled,True) self.settings.setAttribute(QtWebKit.QWebSettings.JavascriptEnabled,True) 

I am doing some tests on the Youtube video page, here is an example of my problems:

image

The second part, which may be related to the first: How can I tell PyQt to wait a few seconds before taking a screenshot? As you can see from the example, the images on the right are still unloaded, because they are loaded using the javascript attribute and data, and in my script I take a screenshot of the signal loadFinished ( onLoad () is the equivalent of javascript)).

My first guess was just

 time.sleep(2) 

Before calling my capture method, but it does not work. I assume that loading Webkit also sleeps during this sleep time, which prevents the page from loading.

I tried to create a custom signal, but then I still do not know how to call it without sleep.

My last assumption is that I need to shed my application. I'm right?

If you have a hint / script to help me display flash content and / or add a setTimeout signal, I would really appreciate it!

Thanks in advance for your help.

EDIT: Just add my solution:

 timeoutTimer = QTimer() timeoutTimer.setInterval(3000) # wait for 3secs timeoutTimer.setSingleShot(True) timeoutTimer.timeout.connect(theMethodToCallOnTimeout) 

About the flash movie: it looks like the flash player is broken on OSX (possibly due to a 32/64 bit problem).

+4
source share
2 answers

If you time.sleep freeze the entire application, you can use QTimer , QEventLoop , QThread , etc., QThread . Here is the version of PyQt4:

 #!/usr/bin/env python #-*- coding:utf-8 -*- from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtWebKit import * class browser(QWebView): def __init__(self, parent=None): super(browser, self).__init__(parent) self.timerScreen = QTimer() self.timerScreen.setInterval(2000) self.timerScreen.setSingleShot(True) self.timerScreen.timeout.connect(self.takeScreenshot) self.loadFinished.connect(self.timerScreen.start) self.load(QUrl("http://www.google.com/ncr")) def takeScreenshot(self): image = QImage(self.page().mainFrame().contentsSize(), QImage.Format_ARGB32) painter = QPainter(image) self.page().mainFrame().render(painter) painter.end() image.save(self.title() + ".png") sys.exit() if __name__ == "__main__": import sys app = QApplication(sys.argv) main = browser() app.exec_() 

QWebView Screenshot

+5
source

I recommend using selenium . It is designed for web automation, screenshots and testing. Since it uses firefox, it is very simple to have full flash support. It can also be started in silent mode. The code below is a screenshot of a flash webpage, such as a youtube video. You can see the screenshot below. A few things to keep in mind. selenium has a save_screenshot method, which you can see is commented out in code. I did not use it because he did not take the correct screenshot of the flash components, you can fix this using the technique in Screenshot of a flash object using Selenium using Webdriver , I chose at least to just take a screenshot with using the imagemagic import command. This is a screenshot tool that works on Linux. You can check the screenshot with python script. [Linux] and Get a screenshot on Windows with Python? for other screenshots approaches.

code

 import time, selenium.webdriver, subprocess browser = selenium.webdriver.Firefox() browser.get('http://www.youtube.com/watch?v=bFEoMO0pc7k') time.sleep(6) #browser.save_screenshot('test.png') subprocess.check_output('import -window root screen.png', shell=True) browser.quit() 

screenshot

enter image description here

+4
source

All Articles