
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Advanced Concepts
- Python - Abstract Base Classes
- Python - Custom Exceptions
- Python - Higher Order Functions
- Python - Object Internals
- Python - Memory Management
- Python - Metaclasses
- Python - Metaprogramming with Metaclasses
- Python - Mocking and Stubbing
- Python - Monkey Patching
- Python - Signal Handling
- Python - Type Hints
- Python - Automation Tutorial
- Python - Humanize Package
- Python - Context Managers
- Python - Coroutines
- Python - Descriptors
- Python - Diagnosing and Fixing Memory Leaks
- Python - Immutable Data Structures
- Python Useful Resources
- Python - Questions & Answers
- Python - Interview Questions & Answers
- Python - Online Quiz
- Python - Quick Guide
- Python - Reference
- Python - Cheatsheet
- Python - Projects
- Python - Useful Resources
- Python - Discussion
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
Python - Automation Tutorial
Automation using Python
Automation with Python involves using programming techniques to perform tasks automatically, typically without human intervention. Python provides various libraries to make it a powerful tool for automating different types of repetitive tasks including, task scheduling, web scraping, GUI automation and many more.
Utilizing the Python's extensive libraries, can create automation solutions to fit specific needs.
Python Libraries for Automation
Following are the few of the Python libraries that are commonly used for automation −
unittest − Unit testing framework in Python.
Selenium − Web automation framework for testing web applications across different browsers.
Beautiful Soup − Library for parsing HTML and XML documents, used for web scraping.
pandas − Data manipulation library, useful for automating data analysis tasks.
requests − HTTP library for sending HTTP requests and handling responses.
Selenium − A web automation framework for testing web applications across different browsers.
PyAutoGUI − GUI automation library for simulating mouse and keyboard actions.
In this tutorial you will learn about the various aspects of automation using Python, from scheduling tasks with schedule module, web scraping with BeautifulSoup, and GUI automation with BeautifulSoup.
Here we are providing practical examples and please ensure that you have the necessary modules installed before executing the provided codes in your local system using the below commands in your command prompt −
pip install schedule pip install beautifulsoup4 pip install pyautogui
Python Automation with "schedule" Module
The schedule module in Python allows scheduling tasks to run automatically at specified intervals or times. It's ideal for automating repetitive tasks such as job scheduling, periodic tasks, or time-based events.
Example
This example demonstrates the application of Python for automating routine tasks such as scheduling weekly meetings, setting alarms, and more, using Python's schedule module.
import schedule import time # Function definitions for scheduled tasks def alarm(): print("Time to restart the server ") def job(): print("Starting daily tasks ") def meet(): print('Weekly standup meeting ') # Schedule tasks schedule.every(5).seconds.do(alarm) # Run every 5 seconds schedule.every().day.at("10:30").do(job) # Run daily at 10:30 AM schedule.every().monday.at("12:15").do(meet) # Run every Monday at 12:15 PM # Main loop to execute scheduled tasks while True: schedule.run_pending() time.sleep(0.5)
Output
On executing the above code we will get the following output −
Time to restart the server Time to restart the server Time to restart the server ...
Python Web Scraping
Web scraping is a straightforward example of automation task. It is a process of automatically extracting information from websites. This technique involves fetching web pages and extracting the required data from the HTML content.
Web scraping is widely used for data mining, data analysis, and automated tasks where collecting information from the web is necessary.
Example
This example demonstrates how to use BeautifulSoup and the urllib.request module to scrape a webpage for URLs containing the keyword "python".
from bs4 import BeautifulSoup from urllib.request import urlopen import re html = urlopen("https://www.tutorialspoint.com/python/index.htm") content = html.read() soup = BeautifulSoup(content, "html.parser") for a in soup.findAll('a',href=True): if re.findall('python', a['href']): print("Python URL:", a['href'])
Output
On executing the above code we will get the following output −
Python URL: /machine_learning_with_python/index.htm Python URL: /python_pandas/index.htm Python URL: /python/index.htm Python URL: /python/index.htm Python URL: /python_pandas/index.htm Python URL: /python_pillow/index.htm Python URL: /machine_learning_with_python/index.htm Python URL: /python_technologies_tutorials.htm Python URL: /python/index.htm ....
Automating Mouse Movements with "pyautogui"
The pyautogui module allows your Python scripts to automate mouse movements, keyboard inputs, and window interactions, useful for testing GUI applications or automating repetitive desktop tasks.
Example
This example demonstrates how the Python's pyautogui module can be used to automate mouse movements in a square pattern.
import pyautogui # Move the mouse in a larger square pattern for i in range(5): pyautogui.moveTo(300, 100, duration=0.25) pyautogui.move(300, 0, duration=0.25) pyautogui.move(0, 300, duration=0.25) pyautogui.move(-300, 0, duration=0.25) pyautogui.move(0, -300, duration=0.25)
Output
On executing the above code we will get the following output −

Automating Unit Testing in Python
The unittest module in Python is used for automated testing. Automated unit testing is the practice of writing tests for individual units of code, such as functions or methods, and running these tests automatically to ensure they behave as expected.
The unittest module in Python is a built-in library that provides a framework for creating and running automated tests.
Example
This example demonstrates how to use the unittest module in Python to perform automated testing on string methods. Each test case method begins with the prefix test_, which is recognized by the unittest framework as a method to run.
# importing unittest module import unittest class TestingStringMethods(unittest.TestCase): # string equal def test_string_equality(self): # if both arguments are then it's succes self.assertEqual('ttp' * 5, 'ttpttpttpttpttp') # comparing the two strings def test_string_case(self): # if both arguments are then it's succes self.assertEqual('Tutorialspoint'.upper(), 'TUTORIALSPOINT') # checking whether a string is upper or not def test_is_string_upper(self): # used to check whether the statement is True or False self.assertTrue('TUTORIALSPOINT'.isupper()) self.assertFalse('TUTORIALSpoint'.isupper()) def test_string_startswith(self): # Used the startswith() to identify the start of the string self.assertTrue('tutorialspoint'.startswith('putor')) self.assertFalse('tutorialspoint'.startswith('point')) # running the tests unittest.main(verbosity=2)
Output
On executing the above code we will get the following output −
test_is_string_upper (__main__.TestingStringMethods) ... ok test_string_case (__main__.TestingStringMethods) ... ok test_string_equality (__main__.TestingStringMethods) ... ok test_string_startswith (__main__.TestingStringMethods) ... FAIL ====================================================================== FAIL: test_string_startswith (__main__.TestingStringMethods) ---------------------------------------------------------------------- Traceback (most recent call last): File "main.py", line 23, in test_string_startswith self.assertTrue('tutorialspoint'.startswith('putor')) AssertionError: False is not true ---------------------------------------------------------------------- Ran 4 tests in 0.000s FAILED (failures=1)