Skip to main content
SeleniumDecoded

Getting Started with Selenium

Learn how to set up Selenium WebDriver and write your first automated test in Java, Python, JavaScript, or C#.

Selenium 3 & 4 Stable

Selenium WebDriver is the most widely-used tool for automating web browsers. In this guide, you’ll learn how to set up your environment and write your first test.

Prerequisites

Before you begin, ensure you have:

  • A modern web browser (Chrome, Firefox, Edge, or Safari)
  • A code editor (VS Code, IntelliJ, PyCharm, etc.)
  • The programming language runtime for your choice of language

Installation

Step 1: Install the Selenium Library

First, add Selenium to your project:

Install Selenium
Selenium 3 & 4
// Add to pom.xml (Maven)
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.18.1</version>
</dependency>
// Or add to build.gradle (Gradle)
implementation 'org.seleniumhq.selenium:selenium-java:4.18.1'
# Using pip
pip install selenium
# Or using pip3
pip3 install selenium
# Verify installation
python -c "import selenium; print(selenium.__version__)"
// Using npm
npm install selenium-webdriver
// Or using yarn
yarn add selenium-webdriver
// Verify installation
node -e "console.log(require('selenium-webdriver/package.json').version)"
// Using NuGet Package Manager
Install-Package Selenium.WebDriver
// Or using .NET CLI
dotnet add package Selenium.WebDriver
// Verify in your .csproj
<PackageReference Include="Selenium.WebDriver" Version="4.18.1" />

Step 2: Download WebDriver

Selenium 4 includes Selenium Manager, which automatically downloads the correct WebDriver for your browser. For Selenium 3, you’ll need to download the driver manually.

BrowserDriverDownload
ChromeChromeDriverchromedriver.chromium.org
FirefoxGeckoDrivergithub.com/mozilla/geckodriver
EdgeEdgeDriverdeveloper.microsoft.com
SafariSafariDriverBuilt into macOS

Your First Test

Let’s write a simple test that opens Google and verifies the page title:

First Selenium Test
Selenium 4 Stable
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTest {
public static void main(String[] args) {
// Selenium 4 auto-manages drivers
WebDriver driver = new ChromeDriver();
try {
// Navigate to Google
driver.get("https://www.google.com");
// Get the page title
String title = driver.getTitle();
System.out.println("Page title: " + title);
// Verify the title
if (title.contains("Google")) {
System.out.println("Test passed!");
} else {
System.out.println("Test failed!");
}
} finally {
// Always close the browser
driver.quit();
}
}
}
from selenium import webdriver
# Selenium 4 auto-manages drivers
driver = webdriver.Chrome()
try:
# Navigate to Google
driver.get("https://www.google.com")
# Get the page title
title = driver.title
print(f"Page title: {title}")
# Verify the title
if "Google" in title:
print("Test passed!")
else:
print("Test failed!")
finally:
# Always close the browser
driver.quit()
const { Builder } = require('selenium-webdriver');
async function firstTest() {
// Selenium 4 auto-manages drivers
const driver = await new Builder().forBrowser('chrome').build();
try {
// Navigate to Google
await driver.get('https://www.google.com');
// Get the page title
const title = await driver.getTitle();
console.log(`Page title: ${title}`);
// Verify the title
if (title.includes('Google')) {
console.log('Test passed!');
} else {
console.log('Test failed!');
}
} finally {
// Always close the browser
await driver.quit();
}
}
firstTest();
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
class FirstTest
{
static void Main(string[] args)
{
// Selenium 4 auto-manages drivers
IWebDriver driver = new ChromeDriver();
try
{
// Navigate to Google
driver.Navigate().GoToUrl("https://www.google.com");
// Get the page title
string title = driver.Title;
Console.WriteLine($"Page title: {title}");
// Verify the title
if (title.Contains("Google"))
{
Console.WriteLine("Test passed!");
}
else
{
Console.WriteLine("Test failed!");
}
}
finally
{
// Always close the browser
driver.Quit();
}
}
}

Key Concepts

WebDriver Instance

The WebDriver interface is your main point of interaction with the browser. Each browser has its own driver implementation:

  • ChromeDriver for Chrome
  • FirefoxDriver for Firefox
  • EdgeDriver for Microsoft Edge
  • SafariDriver for Safari

Use these methods to navigate:

MethodDescription
get(url)Navigate to a URL
navigate().back()Go back in browser history
navigate().forward()Go forward in browser history
navigate().refresh()Refresh the current page

Cleanup

Always call driver.quit() when you’re done. This:

  1. Closes all browser windows
  2. Ends the WebDriver session
  3. Frees up system resources

Important: Use quit() not close(). The close() method only closes the current window, while quit() properly terminates the session.

Next Steps

Now that you have Selenium working, learn about: