Taking screenshots

Finally, we would see how to take a screenshot using pyAutoGUI using the .screenshot() function. We would start by importing the pyAutoGUI module. Then we use the .screenshot() function that takes a screenshot of the present window and stores it as “123.png” in the same directory, for storing in another directory, we need to provide its relative or absolute path. The implementation would be as follows:

Syntax: pyautogui.screenshot()

Parameters: The function has one optional parameter which is the path of the file along with the filename in which the screenshot needs to be stored.

Return Type: The function doesn’t return anything but takes a screenshot and stores it in the path passed inside it as a parameter.

Below is the implementation:

Python3




import pyautogui
 
# takes a screenshot of the present
# window and stores it as "123.png"
pyautogui.screenshot("123.png")


Output:



GUI Automation using Python

In this article, we will explore how we can do GUI automation using Python. There are many modules that can do these things, but in this article, we will use a module named PyAutoGUI to perform GUI and desktop automation using python. 

We would explore two sections –

  • How to automatically use the mouse pointer to perform certain tasks like moving the cursor, clicking on a certain point on the screen, etc
  • Also, we would explore how we can automate the keyboard keystrokes.

Similar Reads

Installation

This module does not come preloaded with Python. To install it type the below command in the terminal....

Getting Started

We should know the screen size of my device before doing any automation. Luckily PyautoGUI helps us to easily get it using the .size() function. It returns a size object with two values that represent the width and height of the screen respectively. Implementation is as follows....

Automating Mouse Movements

...

Automating Keyboard

Getting the current position of the mouse cursor:...

Displaying message boxes

...

Taking screenshots

...