Home > OS >  Screenshot with selenium and python
Screenshot with selenium and python

Time:10-05

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!

CodePudding user response:

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')

CodePudding user response:

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.

  • Related