In today’s fast-paced digital world, automation has become an integral part of software testing. This Selenium Python tutorial will walk you through how to set up and use Selenium with Python for web automation. Selenium is one of the most preferred frameworks because it supports multiple programming languages, including C#, Java, Perl, Ruby, JavaScript, and Python.
Selenium in Python is an open-source toolset that efficiently lets you automate web applications. When combined with Python, a powerful and easy-to-learn programming language, Selenium becomes even more effective for testing.
This guide is a Python Selenium tutorial, a comprehensive introduction to Selenium with Python, which will help you begin with your first automation script.
What is Selenium?
Selenium is an open-source automation framework that helps automate web applications across different browsers and platforms. It offers a suite of tools that include-
- Selenium WebDriver – Automates interactions with web applications.
- Selenium IDE – It’s a browser extension that helps in recording and playback of tests.
- Selenium Grid – It helps run multiple tests in parallel on multiple machines and browsers.
In this article, we will see how to use Selenium Python to automate tests using Selenium WebDriver Python.
Setting Up Selenium With Python
Before starting to write our automated test scripts, we need to set up our development environment.
Installing Python
First, you need to ensure that Python is installed in your system. You may download its latest version from the official Python website.
To verify the installation, open the terminal or command prompt and enter the command below:
python3 --version
You will see the version of Python installed in your system.

Installing Selenium
Next, you can install the Selenium package using the pip installer. For this, enter the command below in the terminal or command prompt:
pip3 install selenium

You can check the version installed for Selenium using the command below:
pip3 freeze | grep “selenium”
Installing IDE- PyCharm
To start writing Python code, you would need an IDE. We will be using PyCharm Community Edition for this tutorial. You can follow the steps on the official website and download the IDE.
Writing Your First Selenium Python Script
Now that your development environment is ready, you are all set to write your first Selenium Python automation test. We will be writing a basic step where we will perform the following steps:
- Open Google Chrome.
- Navigate to www.google.com.
- Capture the title of the web page.
- Close the browser driver.
Let us look at the steps to start our first Selenium test script with Python.
- Upon launching PyCharm, navigate to File and then click on New Project.

- Next, enter the desired name for the project and click on the Create button. You may also select the Location for your project.
- Now you can create your first test in the .py file. All you need to do is right-click on your project and click on New and then select Python File.
- Once your file is created, you can start writing your first Python Selenium script step by step. Below is the code for the simple test case discussed above.
#Imports required libraries from Selenium
from selenium import webdriver
#Initializing webdriver
driver = webdriver.Chrome()
#Navigating to google.com
driver.get("https://www.google.com")
#Fetching title and printing it
print(driver.title)
#Closing webdriver instance
driver.quit()
Now, run the test and see the results to see the Chrome browser launching, the page title being printed in the console window, and finally the browser instance closing down.
Basic Selenium Commands Using Python
Opening a Web Page
We use the basic get() method to open a web page using Selenium.
driver.get(“https://www.google.com”)
Locating Web Elements
Actions and interactions on a web page can be performed only after locating the web elements. Suppose an element is defined as –
<input type="text" name="password" id="pass-id" />
Selenium offers various locating strategies as discussed below-
- Finding elements by ID
Syntax-
driver.find_element(By.ID,"id of the web element")
Example-
driver.find_element(By.ID,"pass-id")
- Finding elements by name
Syntax-
driver.find_element(By.NAME,"Name of the web element")
Example-
driver.find_element(By.NAME,"password")
- Finding elements by XPath
Syntax-
driver.find_element(By.XPATH,"XPath of the web element")
Example-
driver.find_element(By.XPATH,"//input[@id=’pass-id’]")
- Finding elements by CSS Selector
Syntax-
driver.find_element(By.CSS_SELECTOR,"CSS Selector of the web element")
Example-
driver.find_element(By.CSS_SELECTOR,"input#pass-id")
Interacting With Web Elements
Once the elements are located, we can perform actions like clicking, sending input, or retrieving text.
- Clicking a button
element.click()
- Entering text
element.send_keys("Text to be entered")
- Retrieving text
text = element.textprint(text)
Handling Dynamic Elements in Selenium Python
In most applications, some web elements appear dynamically, and to interact with them, Selenium in Python needs to use waits. To handle such elements, we can use implicit and explicit waits.
- Implicit Wait
The code below is for the webdriver to wait for 10 seconds before executing the next line of code. Note that once an implicit wait is applied, it is set for the life of the webdriver object.
driver.implicitly_wait(10)
- Explicit Wait
It waits for a defined condition to occur before proceeding further. In the code below, the webdriver will wait for 10 seconds for the element to match the criteria, and if no element is found, a Timeout exception will be thrown.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Initialize the Chrome WebDriver
driver = webdriver.Chrome()
# Navigate to Google's homepage
driver.get("https://www.google.com")
# Wait up to 10 seconds for the element with ID 'Dynamic Element ID' to appear
wait = WebDriverWait(driver,10)
element = wait.until(EC.presence_of_element_located(By.ID,"Dynamic Element ID"))
Handling Alerts, Frames, and Windows
- Handling Alerts
alert = driver.switch_to.alert
alert.accept() #Clicks on OK
alert.dismiss() # Clicks on Cancel
- Handling Frames
driver.switch_to.frame("Frame name")
- Handling Windows
handles = driver.window_handles
driver.switch_to.window(handles[1]) # Switch to the second window (index 1), assuming a new tab was opened.
Taking Screenshots
The code below allows you to take a screenshot of the current page.
driver.save_screenshot("Screenshot.png")
Using PyTest in Selenium Python
PyTest is a Python testing framework often used along with Selenium Python to write automation test scripts. It helps in efficient test execution and reporting. Let’s have a glance at how you can use it for the same test that we wrote above.
Installing PyTest
You can install PyTest on your Python environment by using the command below in the command line or terminal of your Python project.
pip install pytest
Writing PyTest Test Case in Selenium Python
We will be using the same test case as we used for our first Selenium test using Python. We will just add verifying the page title in this test. The code would look like below-
import pytest
from selenium import webdriver
def test_google_title():
# Initialising webdriver
driver = webdriver.Chrome()
# Navigating to google.com
driver.get("https://www.google.com")
#Fetching the page title and asserting its value
assert "Google" in driver.title
#Quitting the Webdriver
driver.quit()
Executing the Test
You need to run the command below in your IDE terminal to execute the test. Note that you can replace your test file name. Additionally, an HTML report will be generated for the test and added to the project path as shown in the screenshots below.
pytest PyTestTestCaseOne.py --html=report.html
If you’re following along, this Selenium Python tutorial ensures you understand not just the basic commands, but also how to structure tests for maintainability and efficiency before diving into frameworks like PyTest.
Best Practices for Selenium Automation Using Python
Selenium Python is an effective combination to automate your tests, but it is always better to consider certain best practices to make your automation framework more robust.
Some key best practices are:
- Instead of using time.sleep() or implicit waits, always prefer using explicit waits to synchronize test execution with web page loading.
- Optimize WebDriver management by avoiding launching a new WebDriver instance for every test. Instead, use PyTest fixtures for efficient test execution.
- Run tests in headless mode for faster execution.
- Use Page Object Model (POM) to organize test scripts by separating page elements from the test logic.
- Leverage Selenium Grid for parallel test execution to run tests on multiple browsers and environments, helping scale your test coverage.
- Capture screenshots upon test failures to assist with debugging.
- Use logging to log test execution for easy debugging.
Following these best practices in Selenium Python ensures your tests are maintainable, scalable, and easier to debug.
Conclusion
This Selenium Python tutorial showed you how to get started with Selenium with Python, from setting up Selenium WebDriver Python to writing your first automation script, handling waits, using PyTest, and applying best practices. With these skills, you’re ready to start building robust automation frameworks using Python and Selenium. We saw basic commands that can help navigate, locate elements, perform actions, handle dynamic elements, frames, windows, and alerts using Python with Selenium. By using the PyTest framework, you can increase the efficiency of your tests and build a robust test automation framework.
You are now fully equipped to start using Selenium with Python and automate your repetitive web tasks efficiently!