Before you can write a single line of automation, you need a working environment: the Selenium library installed, a browser driver that matches your browser version, and a project structure that keeps things organised. This lesson walks through the complete setup for both Python and Java — the two most popular languages for Selenium automation — so you can choose whichever fits your team and run your first test within minutes.
Setting Up Selenium — Python and Java
Modern Selenium (version 4.6+) includes Selenium Manager, which automatically downloads the correct browser driver for you. This eliminates the most painful part of the old setup process — manually matching ChromeDriver versions to your Chrome browser.
# ── PYTHON SETUP ──
# Step 1: Create a virtual environment (recommended)
# $ python -m venv venv
# $ source venv/bin/activate (macOS/Linux)
# $ venv\Scripts\activate (Windows)
# Step 2: Install Selenium
# $ pip install selenium
# Step 3: Verify installation
from selenium import webdriver
from selenium.webdriver.common.by import By
# Selenium 4.6+ includes Selenium Manager — no manual driver download needed
driver = webdriver.Chrome() # Automatically finds or downloads ChromeDriver
# Verify it works
driver.get("https://www.google.com")
print(f"Page title: {driver.title}")
print(f"Current URL: {driver.current_url}")
print(f"Browser: {driver.capabilities['browserName']}")
print(f"Browser version: {driver.capabilities['browserVersion']}")
driver.quit()
print("Setup verified successfully!")
// ── JAVA SETUP ──
// Step 1: Create a Maven project and add dependency to pom.xml
// <dependency>
// <groupId>org.seleniumhq.selenium</groupId>
// <artifactId>selenium-java</artifactId>
// <version>4.18.1</version>
// </dependency>
// Step 2: Write a verification test
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SetupVerification {
public static void main(String[] args) {
// Selenium Manager handles ChromeDriver automatically
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
System.out.println("Title: " + driver.getTitle());
System.out.println("URL: " + driver.getCurrentUrl());
driver.quit();
System.out.println("Setup verified successfully!");
}
}
webdriver-manager (a third-party Python package), those steps are no longer necessary with modern Selenium. Let Selenium Manager handle it unless you have a specific reason to manage drivers yourself.options = webdriver.ChromeOptions() then options.add_argument("--headless=new") then driver = webdriver.Chrome(options=options). Headless mode is also essential for CI/CD pipelines where no display is available.driver.quit() when your test finishes — in both the success and failure paths. If you forget, browser processes and driver executables accumulate as zombie processes, consuming memory and eventually causing “session not created” errors. In a test framework like pytest, use fixtures with yield or teardown to guarantee cleanup. In Java with JUnit, use @AfterEach or @AfterAll annotations.Common Mistakes
Mistake 1 — Manually downloading ChromeDriver when using Selenium 4.6+
❌ Wrong: Downloading ChromeDriver from the internet, placing it in your PATH, and updating it every time Chrome auto-updates.
✅ Correct: Using Selenium 4.6+ which includes Selenium Manager to automatically detect your browser version and download the matching driver. Simply write driver = webdriver.Chrome() and let Selenium handle the rest.
Mistake 2 — Not using a virtual environment for Python projects
❌ Wrong: Installing Selenium globally with pip install selenium, causing version conflicts across projects.
✅ Correct: Creating a virtual environment (python -m venv venv) for each project, activating it, and then installing dependencies. This isolates each project’s libraries and prevents version conflicts.