Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Check out the best Python automation scripts to boost productivity for developers and testers, covering tasks like handling WebElements and testing.
January 11, 2026
Automated test scripts help ensure websites and web applications function as intended, and Python is a strong choice for writing these scripts.
With its simple syntax and powerful libraries, Python makes it easy to automate tasks like detecting broken links, handling dynamic content, managing pop-ups, and more. When combined with automation testing tools like Selenium, Python automation scripts further enhance the testing process, making it even more efficient and effective.
In this blog, let’s cover some of the best Python automation scripts that can help developers and testers in automating real-life scenarios.
Python is a top choice for test automation due to its simplicity, readability, and rich set of libraries. Its clear syntax allows testers to write scripts quickly.
To run the Python automation scripts, ensure Python is installed on your system (Windows, macOS, or Linux). For Python automation testing, you can run tests on local grids or use cloud-based grids such as TestMu AI for better scalability and reliability.
For this blog, let’s use an AI-driven test execution platform like TestMu AI to perform Selenium Python testing on the cloud. This will help you test websites in different browsers and operating systems.
Now, install the necessary packages using the requirements.txt file by running the below command:
pip install -r requirements.txt
After that, get your TestMu AI Username and Access Key under your Account Settings > Password & Security, which you’ll need for the .env file.
The pyunitsetup.py file contains setup and teardown methods using PyUnit for initializing and closing the Selenium WebDriver.
The environ.get() method is used to retrieve environment variables LT_USERNAME and LT_ACCESS_KEY for authentication. An instance of the EdgeOptions() method is created to configure specific options for the Microsoft Edge browser.
The TestMu AI options in lt_options are set as capabilities using the set_capability() method, and the browser is initialized as a remote WebDriver instance using webdriver.Remote() method.
# Set LambdaTest options
lt_options = {
'build': 'Best 13 Python Automation Scripts',
'project': 'Project: Best 13 Python Automation Scripts',
'name': 'Test: Best 13 Python AutomationScripts',
'platform': 'Windows 10',
'browserName': 'MicrosoftEdge',
'version': 'latest',
'visual': True, # Enable visual testing
'network': True, # Enable network capture
'console': True, # Enable console logs
'video': True, # Enable video recording
'timezone': 'UTC' # Set timezone
}
# Initialize Edge browser with LambdaTest options
edge_options = EdgeOptions()
edge_options.set_capability('LT:Options', lt_options)
self.driver = webdriver.Remote(
command_executor=f"https://{lt_username}:{lt_access_key}@hub.lambdatest.com/wd/hub",
options=edge_options
)

The setUp() method performs setup actions before each test case execution. It sets an implicit wait of 10 seconds and maximizes the browser window. The tearDown() method is called after each test case execution to perform cleanup actions.
The requests library in Python is used for checking the status code of the requests. We will be using the PyUnit framework.
You can also explore the video tutorial below to run automated tests using Python on the TestMu AI platform.
Subscribe to the TestMu AI YouTube Channel for more such tutorials.
Once the above prerequisites are set up, you can use the below-mentioned Python automation scripts for your specific use cases.
Broken links on a website can lead to an unpleasant user experience and negatively impact SEO as well. Detecting broken links is essential for ensuring the integrity and functionality of a website.
The below Python automation script with Selenium finds broken links. It navigates to the TestMu AI eCommerce Playground website for detecting broken links. All links on the page are then extracted using the find_elements() method.
from pyunitsetup import PyUnitTestSetup
from selenium.webdriver.common.by import By
import requests
# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()
setup.setUp() # Added setUp() method call here
try:
# Open the specified URL
setup.driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=product/category&path=57")
# Extract all the links on the page
links = setup.driver.find_elements(By.TAG_NAME, 'a')
# Iterate through each link and check its status code
for link in links:
url = link.get_attribute('href')
if url: # Check if the URL is not empty
try:
response = requests.head(url)
if response.status_code != 200:
print(f"Broken Link Found: {url} - Status Code: {response.status_code}")
except requests.exceptions.MissingSchema:
print(f"Invalid URL: {url} - No scheme supplied.")
except requests.exceptions.RequestException as e:
print(f"Error accessing URL: {url} - {e}")
print("Broken links checked successfully.")
except Exception as e:
print("Error occurred while checking broken links:", e)
finally:
# Close the browser
setup.tearDown()
Each link’s status code is verified by sending a HEAD request using the requests library. Any link returning a status code other than 200 is identified and printed as a broken link.
For more details, check out this blog on finding broken links using Selenium.
Date pickers are commonly used in web applications for selecting dates in forms or other input fields. Automating interactions with date pickers can streamline testing processes and ensure the functionality of date-related features.
from pyunitsetup import PyUnitTestSetup
from selenium.webdriver.common.by import By
from datetime import datetime
# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()
try:
# Open the website with the date picker
setup.driver.get("https://www.lambdatest.com/selenium-playground/bootstrap-date-picker-demo")
# Find the date input field
date_input = setup.driver.find_element(By.XPATH, "//input[@id='sandbox-container1']/input")
# Clear any existing date
date_input.clear()
# Send keys to the date input field to set a new date
date_input.send_keys("02/15/2024")
# Press Enter key to close the date picker
date_input.send_keys(Keys.ENTER)
# Get the selected date from the input field
selected_date = date_input.get_attribute("value")
# Convert the selected date string to a datetime object
selected_date_obj = datetime.strptime(selected_date, "%m/%d/%Y")
# Compare the input and output dates
expected_date = datetime(2024, 2, 15)
if selected_date_obj == expected_date:
print("Date selected successfully.")
else:
print("Error: Selected date does not match expected date.")
except Exception as e:
print("Error occurred while selecting date:", e)
finally:
# Close the browser
setup.tearDown()
The above Python automation script navigates to the Bootstrap Date Pickers Demo page of the TestMu AI Selenium Playground. Then, it locates the date input field and sets a specific date (e.g., “02/15/2024”) by sending keys and simulates pressing the Enter key to close the date picker.
For further information, take a look at this blog on using date pickers in Selenium.
Dynamic content on websites, such as lazy-loaded images or asynchronously loaded elements, can pose challenges for automated testing. Selenium with Python enables us to interact with and validate dynamic content, ensuring that web pages behave as expected under various conditions.
The below Python automation script shows how to handle dynamic content.
from pyunitsetup import PyUnitTestSetup
from selenium.webdriver.common.by import By
# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()
try:
# Open the website with dynamic content
setup.driver.get("https://scrapingclub.com/exercise/list_infinite_scroll/") # Or any other URL with dynamic content
# Verify the presence of dynamic content
dynamic_content = setup.driver.find_elements(By.CLASS_NAME, "lazyload")
if dynamic_content:
print("Dynamic content loaded successfully.")
else:
print("No dynamic content found.")
except Exception as e:
print(f"Failed to load dynamic content: {e}")
finally:
# Close the browser
setup.tearDown()
First, we initialize the Selenium WebDriver with the pyunitsetup file and navigate to a web page containing dynamic content such as lazy-loaded images. Then, it waits for the dynamic content to load, and once loaded, it verifies the behavior of the dynamic elements.
Web pages often contain iFrames (inline frames) that embed another HTML document within the current document.
This Python automation script switches between the main content and iFrames.
from pyunitsetup import PyUnitTestSetup
from selenium.webdriver.common.by import By
# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()
try:
# Open the website with the iframe demo
setup.driver.get("https://www.lambdatest.com/selenium-playground/iframe-demo/")
# Switch to the iframe by name
iframe_name = "framename"
setup.driver.switch_to.frame(iframe_name)
# Perform actions inside the iframe
# For example, you can interact with elements inside the iframe:
iframe_heading = setup.driver.find_element(By.XPATH, "//h2[contains(text(), 'This is inside an iframe')]")
print("Content inside the iframe:", iframe_heading.text)
# Switch back to the main content
setup.driver.switch_to.default_content()
print("iFrame handling successful.")
except Exception as e:
print("Error occurred while handling iFrame:", e)
finally:
# Close the browser
setup.tearDown()
First, it navigates to a Simple iframe page of the TestMu AI eCommerce Playground. Then, switch to the iframe by its name using switch_to.frame() method, and perform actions inside the iframe, such as locating and printing the heading. To switch back to the main content, use switch_to.default_content() method.
You can find additional information in this blog on handling Frames and iFrames in Selenium.
Modals are used in websites to display information, capture user input, or confirm actions. Handling modals is a crucial aspect of web automation to ensure smooth user interactions.
The Python automation script below handles the modal boxes.
from pyunitsetup import PyUnitTestSetup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()
try:
# Open the website with the modal demo
setup.driver.get("https://www.lambdatest.com/selenium-playground/bootstrap-modal-demo/")
# Click on the button to open the modal
open_modal_button = WebDriverWait(setup.driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Launch demo modal')]"))
)
open_modal_button.click()
# Wait for the modal to be visible
modal = WebDriverWait(setup.driver, 10).until(
EC.visibility_of_element_located((By.ID, "myModal"))
)
# Find and print the modal title
modal_title = modal.find_element(By.XPATH, "//div[@class='modal-header']/h4")
print("Modal Title:", modal_title.text)
# Close the modal by clicking the close button
close_button = modal.find_element(By.XPATH, "//button[@class='close']")
close_button.click()
print("Modal handling successful.")
except Exception as e:
print("Error occurred while handling modal:", e)
finally:
# Close the browser
setup.tearDown()
The main code logic is wrapped within a try-except block to handle any exceptions that may occur during execution. Next, the script navigates to a Bootstrap Modal page of the TestMu AI Selenium Playground.
It locates and clicks the button responsible for opening the modal, ensuring it’s clickable through an explicit wait. Then it waits for the modal to become visible before retrieving and printing its title. The tearDown() method is invoked to close the WebDriver session.
Discover more by checking out this blog on handling modal dialog boxes in Selenium.
Popups and alerts on websites are used to convey important information or prompt user actions. Handling popups and alerts effectively is crucial for web automation to ensure smooth user interactions.
from pyunitsetup import PyUnitTestSetup
from selenium.webdriver.common.alert import Alert
# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()
try:
# Open the website with pop-ups/alerts
setup.driver.get("https://ecommerce-playground.lambdatest.io/")
# Click on a button that triggers a pop-up
setup.driver.find_element_by_xpath("//button[contains(text(),'Show Alert')]").click()
# Switch to the alert
alert = Alert(setup.driver)
# Get the text from the alert
alert_text = alert.text
print("Alert text:", alert_text)
# Accept the alert
alert.accept()
print("Alert accepted successfully.")
except Exception as e:
print("Error occurred while handling alert:", e)
finally:
# Close the browser
setup.tearDown()
The main code logic in this script is enclosed within a try-except block to handle any potential exceptions during execution. The script opens a website featuring popups and alerts. It locates and clicks on a button that triggers a popup using an XPath locator.
Upon encountering the alert, it switches to the alert using the alert class. The script retrieves the text from the Alert, accepts it, and prints the alert text.
Get more details from these blogs on handling authentication popups in Selenium and handling alert windows in Selenium.
The Shadow DOM is a part of the website that provides encapsulation by scoping elements and their CSS styles. Handling elements within the Shadow DOM is essential for testing modern web applications that use Shadow DOM for component encapsulation.
This Python automation script navigates to the Shadow DOM of the TestMu AI Selenium Playground that features elements within the Shadow DOM. Using JavaScript execution, it locates a specific element within the Shadow DOM and retrieves its content.
from pyunitsetup import PyUnitTestSetup
# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()
try:
# Open the website with Shadow DOM
setup.driver.get("https://www.lambdatest.com/selenium-playground/shadow-dom")
# Execute JavaScript to find and print the content of the Shadow DOM element
shadow_dom_content = setup.driver.execute_script("return document.querySelector('my-app').shadowRoot.querySelector('h3')")
print("Shadow DOM Content:", shadow_dom_content.text)
print("Shadow DOM handling successful.")
except Exception as e:
print("Error occurred while handling Shadow DOM:", e)
finally:
# Close the browser
setup.tearDown()
To learn more, refer to this blog on handling Shadow DOM in Selenium.
Efficiently managing multiple browser tabs or windows is crucial in web automation, especially when testing complex web applications. The ability to seamlessly navigate between different tabs or windows allows testers to simulate user interactions across various parts of a web application.
import time
from pyunitsetup import PyUnitTestSetup
# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()
try:
# Open the first website in the main tab
setup.driver.get("https://ecommerce-playground.lambdatest.io/")
# Wait for the website to load completely
time.sleep(3)
# Open a new tab
setup.driver.execute_script("window.open('about:blank', '_blank');")
# Wait for a few seconds to let the new tab open
time.sleep(2)
# Switch to the new tab
setup.driver.switch_to.window(setup.driver.window_handles[1])
# Navigate to the second website
setup.driver.get("https://scrapingclub.com/exercise/list_infinite_scroll/")
# Wait for the website to load completely
time.sleep(3)
# Switch back to the main tab
setup.driver.switch_to.window(setup.driver.window_handles[0])
# Open another new tab
setup.driver.execute_script("window.open('about:blank', '_blank');")
# Wait for a few seconds to let the new tab open
time.sleep(2)
# Switch to the new tab
setup.driver.switch_to.window(setup.driver.window_handles[1])
# Navigate to the third website
setup.driver.get("https://www.lambdatest.com/selenium-playground/bootstrap-date-picker-demo")
# Wait for the website to load completely
time.sleep(3)
# Switch back to the main tab
setup.driver.switch_to.window(setup.driver.window_handles[0])
print("Tabs switched successfully.")
except Exception as e:
print(f"Error occurred while switching tabs: {e}")
finally:
# Close the browser
setup.tearDown()
This script opens the TestMu AI eCommerce Playground website in the main tab using the get() method. Then, it opens the ScrapingClub website in a new tab using a window.open() method of JavaScript. After waiting for a few seconds to ensure the new tab is fully loaded, it switches to the new tab using the switch_to.window() method.
Looking for more details? Read this blog on how to switch tabs in Selenium.
UI regression is crucial for ensuring that changes made to a web application do not adversely affect its user interface and functionality.
This Python automation script performs UI regression testing by verifying the presence and visibility of key UI elements on a web page.
from pyunitsetup import PyUnitTestSetup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()
try:
# Maximize the browser window
setup.driver.maximize_window()
# Open the specified URL
setup.driver.get("https://www.lambdatest.com/smart-visual-ui-testing")
# Wait for the main heading to be visible
main_heading = WebDriverWait(setup.driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//h1[contains(text(), 'Smart Visual UI Testing')]"))
)
assert main_heading.is_displayed(), "Main heading 'Smart Visual UI Testing' not found"
# Wait for the input field to be visible
input_field = WebDriverWait(setup.driver, 10).until(
EC.visibility_of_element_located((By.ID, "search_input"))
)
assert input_field.is_displayed(), "Input field not found"
# Wait for the search button to be visible
search_button = WebDriverWait(setup.driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//button[@class='btn btn-primary']"))
)
assert search_button.is_displayed(), "Search button not found"
# Wait for the navigation menu to be visible
navigation_menu = WebDriverWait(setup.driver, 10).until(
EC.visibility_of_element_located((By.ID, "primary_navbar"))
)
assert navigation_menu.is_displayed(), "Navigation menu not found"
# Wait for the footer to be visible
footer = WebDriverWait(setup.driver, 10).until(
EC.visibility_of_element_located((By.ID, "footer"))
)
assert footer.is_displayed(), "Footer not found"
print("All UI elements verified successfully.")
except Exception as e:
print(f"UI regression test failed: {e}")
finally:
# Close the browser
setup.tearDown()
It initializes the WebDriver and navigates to the TestMu AI Visual Regression Testing page. Using the WebDriverWait() method, it waits for important UI elements such as the main heading, input field, search button, navigation menu, and footer to become visible on the page. Once each element is found, it asserts its visibility.
Note: Automate regression tests across 3000+ real environments. Try TestMu AI Now!
Web scraping involves extracting data from websites, enabling the collection of specific information for various purposes, such as data analysis, research, or content aggregation. Python offers libraries like BeautifulSoup that simplify extracting data from web pages.
This script uses the BeautifulSoup library to scrape product categories from the TestMu AI eCommerce website. It sends a GET request to retrieve the HTML content of the page. Then iterates over each product link, extracts the product category, and writes it to a CSV file.
import csv
import requests
from bs4 import BeautifulSoup
from pyunitsetup import PyUnitTestSetup
# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()
# URL of the website to scrape
url = "https://ecommerce-playground.lambdatest.io/"
try:
# Send a GET request to the URL
response = setup.driver.get(url)
# Parse the HTML content
soup = BeautifulSoup(setup.driver.page_source, "html.parser")
# Find all product containers
product_links = soup.select('nav.vertical a.nav-link')
# Define the filename for the CSV file
filename = "product_categories.csv"
# Open the CSV file in write mode and create a CSV writer object
with open(filename, "w", newline="", encoding="utf-8") as csv_file:
writer = csv.writer(csv_file)
# Write the header row
writer.writerow(["Category"])
# Iterate over each product link and extract product category
for link in product_links:
# Extract product category
category = link.select_one('.title').text.strip()
# Write product category to the CSV file
writer.writerow([category])
print("Product categories have been saved to", filename)
except Exception as e:
print("Error occurred while scraping:", e)
finally:
# Close the browser
setup.tearDown()
For an in-depth explanation, read this blog on web scraping with Python.
Web tables are used to display structured data on web pages, such as lists of products or user information.
This Python automation script interacts with tables on a web page. The WebDriverWait() method is used to wait for the table to load on the web page. Once the table is present, it finds all table rows using XPath. Then, iterates over each row and prints its content.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pyunitsetup import PyUnitTestSetup
# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()
# Open the website with the table pagination demo
setup.driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")
try:
# Wait for the table to load
WebDriverWait(setup.driver, 10).until(EC.presence_of_element_located((By.ID, "myTable")))
# Find all table rows
rows = setup.driver.find_elements(By.XPATH, "//table[@id='myTable']/tbody/tr")
# Print the content of each row
for row in rows:
print(row.text)
print("Web table handled successfully.")
except Exception as e:
print("Error occurred while handling web table:", e)
finally:
# Close the browser
setup.tearDown()
For a comprehensive guide, read this blog on handling web tables in Selenium.
Discover the top Python frameworks to watch out for in 2025. Stay updated on the tools shaping the future of development.
With the rapid advancements in technology, automation is here to stay. Python offers an extensive array of libraries and tools that empower users to automate a wide range of tasks, from simple operations to complex processes. This can be achieved by writing Python automation scripts with tools like Selenium.
So, whether you’re a beginner or an experienced developer or tester, mastering Selenium Python for writing automation scripts can significantly enhance your workflow and open doors to new possibilities.
Author
Did you find this page helpful?
More Related Hubs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance