Create a Simple Web Automation Script with Python Selenium

In this tutorial, we will walk through the process of creating a simple web automation script using Python and Selenium. Selenium is an open-source web testing library that provides a convenient API for automating browsers. It allows you to control browsers programmatically, making it an excellent tool for web scraping, automated testing, and automating repetitive tasks.

Prerequisites

Before we begin, ensure that you have the following installed on your system:

  1. Python (version 3.6+): Download and install the latest version of Python from the official website.
  2. pip: pip is the package installer for Python, which should come pre-installed with Python 3.4+.

Installing Selenium

To install Selenium, simply run the following command in your terminal or command prompt:

pip install selenium

Additionally, you will need to download the appropriate WebDriver for your browser. In this tutorial, we will be using Google Chrome. Download the ChromeDriver that corresponds to your installed version of Google Chrome.

Writing the Web Automation Script

Let's create a simple script that automates a Google search.

  1. Import the necessary modules:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
  1. Set up the WebDriver and navigate to the Google homepage:
# Replace the path with the path to your ChromeDriver
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')

driver.get('https://www.google.com')
  1. Locate the search bar and enter a query:
search_bar = driver.find_element_by_name('q')
search_bar.send_keys('Python Selenium')
search_bar.send_keys(Keys.RETURN)
  1. Wait for the search results to load and close the browser:
time.sleep(5)
driver.quit()
  1. Put it all together:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Replace the path with the path to your ChromeDriver
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')

driver.get('https://www.google.com')

search_bar = driver.find_element_by_name('q')
search_bar.send_keys('Python Selenium')
search_bar.send_keys(Keys.RETURN)

time.sleep(5)
driver.quit()

Save the script as google_search.py and run the script using the following command:

python google_search.py

If everything is set up correctly, the script will open Google Chrome, navigate to the Google homepage, enter the search query "Python Selenium," and then close the browser after displaying the search results.

Conclusion

In this tutorial, we learned how to create a simple web automation script using Python and Selenium. This is just the tip of the iceberg when it comes to the capabilities of Selenium. You can use it to automate form submissions, interact with dynamic web content, and much more. The next time you find yourself performing repetitive tasks in a web browser, consider automating the process with Selenium!

An AI coworker, not just a copilot

View VelocityAI