1

I need to take a screenshot of my entire screen for some automated tests I need to perform.

I was able to do this, using driver.get_screenshot_as_file , but the problem is that it only takes the picture of the web page, I need to get the whole picture from the browser, since the data I need to check is in the devtools.

Pic: enter image description here

I need this:

enter image description here

Thankss!

CM10
  • 11
  • 1

2 Answers2

0

You can use the package pyautogui to get screenshot of the desktop on the os level. This take screenshot of the entire desktop rather than just the webpage.

import pyautogui

pyautogui.screenshot().save('screenshot.png')
Tim
  • 3,178
  • 1
  • 13
  • 26
  • Great this library, I didn't know it. I get the following error, what can it be due to? I install pillow (pip install Pillow --upgrade) pyautogui.screenshot().save('aut.jpg') File "C:\Users\asd\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pyscreeze\__init__.py", line 144, in wrapper raise PyScreezeException('The Pillow package is required to use this function.') pyscreeze.PyScreezeException: The Pillow package is required to use this function. – CM10 Oct 04 '22 at 20:41
  • what version of Pillow do you now have? – Tim Oct 04 '22 at 20:51
  • can you try this in a clean virtual environment? I just tried it with `pip install Pillow --upgrade` and it works – Tim Oct 04 '22 at 21:13
0

Another alternative to pyautogui would be PIL's ImageGrab. The advantage is that you are able to specify a bounding box:

from PIL import ImageGrab
image = ImageGrab.grab(bbox=None) # bbox=None gives you the whole screen
image.save("your_browser.png")

# for later cv2 use:
import numpy
import cv2
image_cv2 = cv2.cvtColor(numpy.array(image), cv2.COLOR_BGR2RGB)

This also makes it possible to adapt to your browser's window size and only capture its specific window. You can get your browsers bounding box as shown in this answer: https://stackoverflow.com/a/3260811/20161430.

From a speed perspective, it doesn't seem to make much of a difference whether you are using pyautogui or PIL.

ximp_
  • 11
  • 3