Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud
  • Home
  • /
  • Blog
  • /
  • 13 Python Automation Scripts for Developers and Testers
AutomationSelenium PythonTutorial

13 Python Automation Scripts for Developers and Testers

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.

Why Automate Tests With Python?

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.

  • Simple and readable syntax: Python makes writing and understanding automation scripts easier, reducing complexity and errors.
  • Rich library support: Python has powerful libraries like Selenium, making it perfect for automating tasks such as handling dynamic content, pop-ups, and WebElements.
  • Seamless integration: Python integrates well with popular automation tools like Selenium, enhancing the efficiency of the testing process.
  • Beginner-friendly yet powerful: Python’s ease of use makes it ideal for beginners, while its advanced features cater to experienced testers as well.

How to Run Python Automation Scripts?

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

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.

13 Best Python Automation Scripts

Once the above prerequisites are set up, you can use the below-mentioned Python automation scripts for your specific use cases.

Handling Date Pickers

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.

Handling Dynamic Content

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.

Handling Cookies

Cookies are small pieces of data that websites store. They are often used to remember user preferences, login information, and other browsing-related data. In web automation, it’s essential to understand how to handle cookies to simulate realistic user behavior.

from pyunitsetup import PyUnitTestSetup
from selenium import webdriver


# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()


try:
    # Open the first website
    setup.driver.get("https://ecommerce-playground.lambdatest.io/")


    # Get all cookies
    cookies = setup.driver.get_cookies()
    print("Initial cookies:", cookies)


    # Add a new cookie
    new_cookie = {'name': 'example_cookie', 'value': '1234567890'}
    setup.driver.add_cookie(new_cookie)


    # Get updated cookies
    updated_cookies = setup.driver.get_cookies()
    print("Updated cookies:", updated_cookies)


    # Delete the added cookie
    setup.driver.delete_cookie('example_cookie')


    # Get final cookies
    final_cookies = setup.driver.get_cookies()
    print("Final cookies:", final_cookies)


    # Open the second website
    setup.driver.get("https://scrapingclub.com/exercise/list_infinite_scroll/")


    # Open the third website
    setup.driver.get("https://www.lambdatest.com/selenium-playground/bootstrap-date-picker-demo")


except Exception as e:
    print("Error occurred:", e)


finally:
    # Close the browser
    setup.tearDown()

This Python automation script first initializes the PyUnitTestSetup() fixture, opens a website, and retrieves all the cookies associated with it. Then, it adds a new cookie to the browser session and retrieves the updated list of cookies to verify that the new cookie has been added.

Want more details? Check out this blog on handling cookies in Selenium.

Handling Menus

Interacting with menus is another common task in web automation. Menus often contain dropdown lists, submenus, and other interactive elements.

This Python automation script shows how to handle menus using the ActionChains class.

from pyunitsetup import PyUnitTestSetup
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By


# Initialize the PyUnitTestSetup fixture
setup = PyUnitTestSetup()


try:
    # Open the website with the menu to be handled
    setup.driver.get("https://ecommerce-playground.lambdatest.io/")


    # Locate the menu element
    menu_element = setup.driver.find_element(By.XPATH, "//span[contains(text(), 'Categories')]")


    # Hover over the menu element to reveal the dropdown
    ActionChains(setup.driver).move_to_element(menu_element).perform()


    # Wait for the dropdown to be visible
    submenu_element = setup.driver.find_element(By.XPATH, "//a[contains(text(), 'Clothing')]")
    submenu_element.click()


    print("Menu handling successful.")


except Exception as e:
    print("Error occurred while handling menus:", e)


finally:
    # Close the browser
    setup.tearDown()

The script locates the menu element on the webpage using XPATH and uses ActionChains to hover over the menu element, which reveals the dropdown. Once the dropdown is visible, it clicks on a specific submenu element (in this case, “Clothing”). If any errors occur during the execution of the script, we catch and handle them appropriately.

Handling iFrames

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.

Handling Modals

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.

Handling Popups and Alerts

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.

Handling Shadow DOM

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.

Switching Tabs and Windows

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.

Testing UI Regression

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

Note: Automate regression tests across 3000+ real environments. Try TestMu AI Now!

Scraping Websites

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.

Handling Web Tables

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.

Conclusion

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

Close

Summarize with AI

ChatGPT IconPerplexity IconClaude AI IconGrok IconGoogle AI Icon

Frequently asked questions

Did you find this page helpful?

More Related Hubs

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests