A
A
Andrew Lays2015-08-09 18:31:32
Python
Andrew Lays, 2015-08-09 18:31:32

How to take screenshot with python script?

There is a python code:

import pyscreenshot as ImageGrab

from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

display = Display(visible=0, size=(1366, 768)).start()
browser = webdriver.Firefox()

browser.get('https://google.com')
browser.implicitly_wait(10)

print browser.title

img = ImageGrab.grab()

img.save('screen.png');

browser.quit()

display.stop()

Its essence is to create a virtual display, open firefox through selenium webdriver, go to the https://google.com page and take a screenshot after 10 seconds. Everything works until the screenshot is taken, because the print browser.title line prints out Google, which is the correct title of the page. The following code throws an error:
Traceback (most recent call last):
  File "script.py", line 13, in <module>
    img = ImageGrab.grab()
  File "/usr/local/lib/python2.7/dist-packages/pyscreenshot/__init__.py", line 70, in grab
    return _grab(to_file=False, childprocess=childprocess, backend=backend, bbox=bbox)
  File "/usr/local/lib/python2.7/dist-packages/pyscreenshot/__init__.py", line 50, in _grab
    backend_obj = _get_loader().selected()
  File "/usr/local/lib/python2.7/dist-packages/pyscreenshot/loader.py", line 75, in selected
    self.raise_exc()
  File "/usr/local/lib/python2.7/dist-packages/pyscreenshot/loader.py", line 83, in raise_exc
    raise FailedBackendError(message)
pyscreenshot.loader.FailedBackendError: Install at least one backend!

The error is not googled, the script is executed under sudo, using selenium webdriver, the screen is made with the following command browser.save_screenshot('path'), but it does not suit me, because it captures only the page area, the browser itself, its tabs, the address bar - not visible. And yet, I'm sure it's possible, because I did it on another machine about a year ago. Ideas?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrew Lays, 2015-08-09
@Andrew_Lays

The answer was simple. I read in the documentation on GitHub that:
And just below:

Plugin based, it has wrappers for various back-ends:
scrot
ImageMagick
PyGTK
PIL or Pillow (only on windows)
PyQt4
wxPython
Quartz (Mac)
screencapture (Mac)

Of these, only Pillow was installed, but the system is not Windows, so it did not work. According to the comment, Vlad Grachev installed ImageMagick, which is included in the list above, which solved the whole problem.

V
Vladlen Grachev, 2015-08-09
@gwer

You can turn to third-party tools, for example. The same imagemagick allows you to take screenshots. Call, for example, via subprocess.call.

D
DENIS SHELESTOV, 2015-08-09
@djdeniro

must be run with administrator rights

A
Andrey K, 2015-08-09
@mututunus

PIL or Pillow installed?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question