
πSelenium Python Tutorial with WebDriver Example
Selenium Python Tutorial: Selenium supports multiple programming languages, and Python stands out due to its simplicity and powerful libraries. With Selenium WebDriver and Python, testers can automate browsers like Chrome, Firefox, and Edge across multiple platforms.
In this tutorial, you’ll learn how to install Python for Selenium, set up the development environment in Eclipse using PyDev, and create real-time automation scripts.
πTable of Contents
- What is Python?
- What is Selenium?
- Why Use Python with Selenium?
- How to Install and Configure PyDev in Eclipse
- Create Selenium Test Scripts with Python
- Selenium Python Example 1: Facebook Login
- Selenium Python Example 2: Login and Validate Page Title
- Python vs Java in Selenium
- Summary
π What is Python?
Selenium Python Tutorial: Python is a high-level, object-oriented programming language designed for simplicity and readability. It uses indentation instead of braces and English-like syntax, making it ideal for beginners and test automation.
Keyword | Meaning | Usage Example |
elif | Else if | if x==1: … elif x==2: … |
else | Else | if x: … else: … |
except | Exception | except ValueError as e: print(e) |
exec | Execute Code | exec(“print(‘Hello World’)”) |
π What is Selenium?
Selenium Python Tutorial: Selenium is an open-source automation testing tool used for web application testing. It simulates user interactions with the browser such as:
- Clicking buttons
- Entering text in fields
- Navigating through links
- Validating website behavior
π Why Use Python with Selenium?
- Selenium Python Tutorial: Python is simpler and less verbose compared to Java or C#.
- Selenium APIs in Python are powerful and easy to integrate.
- Python can interact with Selenium WebDriver across all browsers like Chrome, Firefox, Edge, and Safari.
- Great for cross-platform test execution.
π How to Install and Configure PyDev in Eclipse
Step 1: Open Eclipse Marketplace
Navigate to Help > Install New Software
Step 2: Add PyDev Repository
Use the URL: http://pydev.org/updates
Select all items and click Next > Accept the license > Finish installation.
Step 3: Handle Security Warning
Click Install Anyway if prompted.
Step 4: Set Python Interpreter
Navigate to: Window > Preferences > PyDev > Interpreter – Python
Click on Browse and select your Python installation path (e.g., python.exe)
Apply and close.
π Create Selenium Test Scripts with Python
Step 1: Create New PyDev Project
Right-click on PyDev Package Explorer β New β Others β PyDev Project
Name your project and click Finish
Step 2: Create Python Package
Right-click on the project β New β PyDev Package β Name it and click Finish
Step 3: Create Python Module
Right-click on the package β New β PyDev Module β Name it and choose “Empty Template”
π Selenium Python Example 1: Facebook Login
Code:
python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
user = “your_username”
password = “your_password”
driver = webdriver.Firefox()
driver.get(“https://www.facebook.com”)
driver.find_element(“id”, “email”).send_keys(user)
driver.find_element(“id”, “pass”).send_keys(password)
driver.find_element(“id”, “pass”).send_keys(Keys.RETURN)
driver.quit()
Explanation:
- Initializes Firefox WebDriver
- Navigates to Facebook
- Inputs username & password
- Simulates ENTER key
- Closes browser
π Selenium Python Example 2: Login and Validate Page Title
Selenium Python Tutorial: python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get(“https://www.facebook.com”)
driver.find_element(By.ID, “email”).send_keys(“your_username”)
driver.find_element(By.ID, “pass”).send_keys(“your_password”)
driver.find_element(By.ID, “pass”).send_keys(Keys.RETURN)
time.sleep(5)
title = driver.title
if “Facebook” in title:
print(“Login Successful. Page title verified.”)
else:
print(“Login Failed or incorrect page title.”)
driver.quit()
π Python vs Java in Selenium
Feature | Python | Java |
Syntax | Simple, compact | Verbose and complex |
Typing | Dynamically typed | Statically typed |
Block Structure | Indentation-based | Braces {} based |
Execution | Faster scripting, less compilation | Slower scripting, requires compilation |
π Summary
- Selenium Python Tutorial: Selenium + Python is a powerful combo for automation testing of web applications.
- You can install PyDev in Eclipse to create Python projects and modules.
- Python provides readable and efficient syntax to write Selenium scripts.
- It supports all major browsers and platforms using standard WebDriver commands.
- Real-time examples like Facebook login demonstrate practical implementation.