Table of contents
1.
Introduction
2.
Installation and setup of Pyautogui
3.
Building a basic Automatic Form Filler
4.
formFiller.py
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Pyautogui - Automatic Form Filler

Introduction

Pyautogui is a Python cross-platform GUI automation module used to simplify or automate some boring stuff like form filling, filling the details such as an address, card details, etc., while ordering a product online. Pyautogui will support both python 2 and python 3.

It is a cross-platform tool because it supports all famous operating systems like Unix, Windows, and Linux. And the term GUI automation defines the nature of developing programs that can control other applications by sending them virtual keystrokes and mouse clicks. It will be like sitting near your computer and interacting with the applications yourself.GUI - Graphical user interface…

Installation and setup of Pyautogui

Before using the Pyautogui automation tool, you need to install and set it up perfectly so that it won’t bother you while using it in the future. 
First, You need to have Python installed on your computer. Here is a beautiful link to help you install and set up Python on your computer.  Install Python

Installing Pyautogui:

On Windows:
Open the terminal, run - > pip install pyautogui       
The above line will install the pyautogui library into your computer.

On Linux:
Before installing pyautogui, you need to install some other dependencies. To do that,
Run - > 
sudo pip3 install python3-xlib
sudo apt-get install scrot
sudo apt-get install python3-tk
sudo apt-get install python3-dev
Then Run -> pip install pyautogui

On OS X:
Before installing pyautogui, you need to install some other dependencies. To do that,
Run - > 
sudo pip3 install pyobjc-framework-Quartz
sudo pip3 install pyobjc-core
sudo pip3 install pyobjc

Then run -> pip install pyautogui
To Use the installed pyautogui, you need to import the module into your python working file.
You can use import pyautogui to do that.
You can learn and use many useful functions to automate the boring stuff that this module will provide. You can learn more about these functions in their official documentation linked.

Building a basic Automatic Form Filler

Let’s say we are trying to automate filling the form like this -> Basic Form that looks like this.

First, we need to figure out what fields are present in the form, and then we need to locate the fields and types of the fields, etc.
For example,
NameFeild is at (648, 319)  # according to the computer on which it is opened, etc.
Then we figure out the whole process that looks like:

  • First, Click the name field, type the Name, and press TAB to move to the next field.
  • Click the Experience field, type the content, and press TAB to move to the following field.
  • Click the Comments field, type the content, and press TAB to move to submit button.
  • At last, press ENTER key to click the submit button to submit the form.

formFiller.py

# First, we need to import the packages pyautogui and time.
import pyautogui, time

#tell the user to press ctrl+c to kill the script.
print(‘Press CTRL-C to stop’)

#setting the coordinates, this will be different to your computer.
nameField = (648, 319)
submitButton = (651, 817)
submitColor = (75, 141, 249)  #this color will say whether the form is submitted or not.

#Take the person data; you can add multiple persons here and loop through this.
personData = {‘name’ : ‘vamsi’, ‘experience’ : ‘Best’, ‘Comments’ : ‘One Of the Best Blog’}

pyautogui.PAUSE = 0.5

#Check or wait until the form page has loaded
while not pyautogui.pixelMatchesColor(submitButton[0], submitButton[1], submitColor):
	time.sleep(0.5)
print(‘Entering the info of %s…’ %(personData[‘name’]))
pyautogui.click(nameField[0], nameField[1])  #Clicking the name field.
pyautogui.typewrite(personData[‘name’] + ‘\t’) 

#the above statement will write the name of the person and press the tab to move to the next field.

#this below conditional statements will fill the experience field
if personData[‘experience’] == ‘Good’:
	pyautogui.typewrite([‘down’, ‘\t’])
elif personData[‘experience’] == ‘Better’:
	pyautogui.typewrite([‘down’, ‘down’, ‘\t’])
elif personData[‘experience’] == ‘Best’:
	pyautogui.typewrite([‘down’, ‘down’, ‘\t’])

#Filling the comments section
pyautogui.typewrite(personData[‘Comments’], ‘\t’)

#Click Submit by pressing the ENTER button
pyautogui.press(‘enter’)
print(‘Clicked Submit’)
You can also try this code with Online Python Compiler
Run Code

Here this formFiller will fill the basic form.

FAQs

  1. Which versions of Python will support the Pyautogui package?
    Both Python version 2x and Python 3x will support Pyautogui. It would be best if you had pip installed regarding your version to install pyautogui. As it is a cross-platform tool, it will support Windows, Linux, and macOS.
  2. How to press the keys in pyautogui?
    Pyautogui will provide a beautiful method called pyautogui.press() function that accepts a string from pyautogui.KEYBOARD_KEYS such as esc, enter, f1. You can go through their official documentation to learn more about it.
  3. How do I click on pyautogui?
    pyautogui.click() method will allow you to pass a virtual mouse click on your computer screen. This method will accept the x and y coordinates of the location where the click is to be done.
  4. What is Pyautogui in Python?
    Pyautogui is a python automation tool that will automate the boring stuff. Pyautogui will send virtual mouse clicks and virtual keypresses to windows, macOS, and Linux. 

Key Takeaways

Pyautogui is a simple python automation tool to automate the boring stuff such as filling forms etc.
In this form, we have used, 

  • pyautogui.typewrite(‘content’) method will write the content to the form or screen.
  • pyautogui.click(‘entity’) method will click the particular entity represented by their coordinates. Or this method will send a virtual mouse click to the computer.
  • pyautogui.press() method will be a mouse event that will be handled with other methods.
  • pyautogui. pixelMatchColor() method will return true if the pixel at the given x and y coordinates on the screen matches the given color.

You can search for more useful methods and play around with them by using their official documentation PyAutoGUI.
Hey Ninjas! You can check out more unique courses on machine learning concepts through our official website, Coding Ninjas, and checkout Coding Ninjas Studio to learn through articles and other important stuff to your growth.

Happy learning!

 

             

 

Live masterclass