Introduction
After installing Python, Selenium, and a browser driver, the next step is to create your first Selenium automation script.
A Selenium script is simply a Python program that uses Selenium WebDriver to automate browser actions such as opening a browser, navigating to a website, interacting with web elements, validating application behavior, and closing the browser.
Your first Selenium script helps you verify that your Selenium environment is configured correctly and introduces the basic workflow followed by almost every Selenium automation test.
In this tutorial, you will learn how to create your first Selenium script, understand each line of code, execute the script, explore real-world use cases, avoid common mistakes, and follow industry best practices.
What is a Selenium Script?
A Selenium script is a Python program that automates user interactions with a web browser.
A typical Selenium script performs the following steps:
-
Launch the browser.
-
Open a website.
-
Perform one or more actions.
-
Validate the expected result.
-
Close the browser.
For example:
Launch Browser
│
▼
Open Website
│
▼
Perform Actions
│
▼
Validate Results
│
▼
Close Browser
Why Create Your First Selenium Script?
Creating your first Selenium script helps you:
-
Verify that Selenium is installed correctly.
-
Confirm that the browser launches successfully.
-
Understand the Selenium WebDriver workflow.
-
Learn the basic structure of Selenium automation scripts.
-
Prepare for writing more advanced automation tests.
Basic Structure of a Selenium Script
Almost every Selenium script follows this structure:
Import Required Modules
│
▼
Create WebDriver Object
│
▼
Open Website
│
▼
Perform Actions
│
▼
Validate Results
│
▼
Close Browser
Practical Example
The following example creates a simple Selenium script that launches Chrome, opens the Selenium practice website, verifies the page title, and closes the browser.
from selenium import webdriver
# Topic: 4. Your First Selenium Script - Creating Your First Selenium Script
# Practice site: https://the-internet.herokuapp.com/
#
# A minimal Selenium script launches a browser, opens a page, and verifies the
# page title before closing the session.
def test_creating_first_selenium_script():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/")
assert "The Internet" in driver.title
finally:
driver.quit()
Output
Browser launched successfully.
Website opened successfully.
Page Title:
The Internet
Assertion Passed.
Test Executed Successfully.
Understanding the Code
Import Selenium WebDriver
from selenium import webdriver
Imports the Selenium WebDriver module required for browser automation.
Create the Chrome WebDriver
driver = webdriver.Chrome()
Launches a new Google Chrome browser session.
Open the Website
driver.get("https://the-internet.herokuapp.com/")
Navigates to the Selenium practice website.
Verify the Page Title
assert "The Internet" in driver.title
Checks whether the browser title contains “The Internet”.
If the assertion passes, the webpage has loaded successfully.
Close the Browser
driver.quit()
Closes all browser windows and ends the WebDriver session.
Execution Flow
Import Selenium
│
▼
Launch Chrome Browser
│
▼
Open Practice Website
│
▼
Read Page Title
│
▼
Verify Title
│
▼
Close Browser
Automation Testing Example
Suppose your company has a login application.
The first automation test may simply verify that the application opens successfully.
driver.get("https://company-app.com")
assert "Login" in driver.title
This confirms that the application is available before performing further testing.
Real-World Example
Before automating an e-commerce website, testers often create a basic script that verifies the homepage is accessible.
driver.get("https://example-store.com")
assert "Store" in driver.title
If the title matches, the environment is ready for additional automation steps such as logging in, searching for products, or placing orders.
Common Mistakes Beginners Make
Forgetting to Close the Browser
Incorrect
driver = webdriver.Chrome()
driver.get("https://the-internet.herokuapp.com/")
The browser remains open after execution.
Correct
driver.quit()
Always close the browser to release system resources.
Forgetting to Import WebDriver
Incorrect
driver = webdriver.Chrome()
Without importing Selenium:
from selenium import webdriver
Python raises a NameError.
Using an Incorrect Website URL
driver.get("https://wrong-url.com")
Always verify that the URL is valid before running the script.
Best Practices
-
Keep your first Selenium script simple.
-
Use
driver.quit()to close the browser after execution. -
Verify page titles or URLs to confirm successful navigation.
-
Organize your code using functions or test methods.
-
Use practice websites while learning Selenium.
Conclusion
Creating your first Selenium script is the foundation of Selenium WebDriver automation. It introduces the basic workflow used in almost every automation test: launching the browser, opening a webpage, validating the result, and closing the browser.
Once you are comfortable with this workflow, you can begin learning browser commands, locators, web element interactions, waits, and automation framework development.
Frequently Asked Questions (FAQs)
What is a Selenium script?
A Selenium script is a Python program that automates browser interactions using Selenium WebDriver.
Why is the first Selenium script important?
It verifies that your Selenium environment is configured correctly and introduces the basic automation workflow.
What does webdriver.Chrome() do?
It launches a new Google Chrome browser session controlled by Selenium.
Why do we use driver.get()?
driver.get() opens the specified website in the browser.
Why should we use driver.quit()?
driver.quit() closes all browser windows and properly ends the WebDriver session.
Key Takeaways
-
A Selenium script automates browser actions using Selenium WebDriver.
-
The first Selenium script verifies that the Selenium environment is working correctly.
-
A typical Selenium script launches the browser, opens a website, validates the result, and closes the browser.
-
webdriver.Chrome()starts a Chrome browser session. -
driver.get()opens the specified webpage. -
Assertions help verify that the webpage has loaded correctly.
-
driver.quit()should always be used to close the browser and end the WebDriver session. -
Understanding your first Selenium script is the first step toward building complete Selenium automation frameworks.
