Getting Started with Selenium
Learn how to set up Selenium WebDriver and write your first automated test in Java, Python, JavaScript, or C#.
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:
// 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 pippip install selenium
# Or using pip3pip3 install selenium
# Verify installationpython -c "import selenium; print(selenium.__version__)"// Using npmnpm install selenium-webdriver
// Or using yarnyarn add selenium-webdriver
// Verify installationnode -e "console.log(require('selenium-webdriver/package.json').version)"// Using NuGet Package ManagerInstall-Package Selenium.WebDriver
// Or using .NET CLIdotnet 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.
| Browser | Driver | Download |
|---|---|---|
| Chrome | ChromeDriver | chromedriver.chromium.org |
| Firefox | GeckoDriver | github.com/mozilla/geckodriver |
| Edge | EdgeDriver | developer.microsoft.com |
| Safari | SafariDriver | Built into macOS |
Your First Test
Let’s write a simple test that opens Google and verifies the page title:
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 driversdriver = 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:
ChromeDriverfor ChromeFirefoxDriverfor FirefoxEdgeDriverfor Microsoft EdgeSafariDriverfor Safari
Navigation
Use these methods to navigate:
| Method | Description |
|---|---|
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:
- Closes all browser windows
- Ends the WebDriver session
- Frees up system resources
Important: Use
quit()notclose(). Theclose()method only closes the current window, whilequit()properly terminates the session.
Next Steps
Now that you have Selenium working, learn about:
- Finding Elements - Locate elements on the page
- Browser Options - Configure headless mode and browser settings
- Basic Interactions - Click, type, and interact with elements
- Explicit Waits - Handle dynamic content reliably
- Project Structure - Organize your test codebase