13 Python Automation Scripts for Developers and Testers
Ekemini samuel
Posted On: September 30, 2024
12 Min
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.
TABLE OF CONTENTS
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 LambdaTest for better scalability and reliability.
For this blog, let’s use an AI-driven test execution platform like LambdaTest 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:
| 
					 1  | 
						pip install -r requirements.txt  | 
					
After that, get your LambdaTest 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 LambdaTest 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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  | 
						# 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 LambdaTest platform.
Subscribe to the LambdaTest 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.
Finding Broken Links
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 LambdaTest eCommerce Playground website for detecting broken links. All links on the page are then extracted using the find_elements() method.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43  | 
						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.
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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53  | 
						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 LambdaTest 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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28  | 
						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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52  | 
						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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37  | 
						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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38  | 
						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 LambdaTest 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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48  | 
						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 LambdaTest 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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35  | 
						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 LambdaTest 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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27  | 
						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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75  | 
						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 LambdaTest 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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64  | 
						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 LambdaTest 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.
Automate regression tests across 3000+ real environments. Try LambdaTest 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 LambdaTest 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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60  | 
						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.
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38  | 
						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.
Frequently Asked Questions (FAQs)
What is a Python automation script?
A Python automation script is a program written to perform repetitive tasks or workflows automatically, such as file handling, web scraping, or data processing. It helps streamline tasks by reducing manual intervention.
Can I automate a Python script?
Yes, you can automate a Python script by scheduling it with tools like Cron (Linux) or Task Scheduler (Windows) or integrating it with automation frameworks to run tasks at specified intervals.
How to write automated test scripts in Python?
To write automated test scripts in Python, use testing frameworks like unittest, pytest, or nose. Define test cases, assertions, and test suites to validate the functionality of your code.
Author
                