Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 35 ยท Getting Started

Setup and Your First Script

Install Selenium bindings for Python, Java or JavaScript, let Selenium Manager fetch the driver, then open a page, read its title and quit cleanly.

Getting started with Selenium used to mean downloading the right driver by hand, putting it on your PATH and hoping the version matched your browser. Modern Selenium does that for you. You install one library for your language, write about five lines, and a real browser opens.

This lesson shows the install and the first script in Python, Java and JavaScript, explains what Selenium Manager does, and teaches the most important habit: always end the session with quit(). Because the real thing needs a browser on your machine, the code here is read-only. Two runnable stand-ins at the end model the driver object and the cost of forgetting to quit.

What you need

Setup checklist

A browser Chrome, Firefox or Edge, installed normally.
A binding The Selenium library for your language.
Selenium Manager Finds or downloads the matching driver for you.
A script Start, navigate, read, quit.

No manual driver download is needed with a current Selenium.

1 / 2

Selenium Manager is a small helper that ships inside the Selenium libraries (from Selenium 4.6 on). When you start a browser, it checks which browser is installed, finds or downloads a matching driver, and caches it. If you run into an unusual setup, such as a locked-down corporate machine without internet access, you can still point Selenium at a driver you downloaded yourself, but most people never need to.

Python

Create a virtual environment (see Python Virtual Environments if that is new) and install the package:

Terminal
pip install selenium

Then write the script:

Python
from selenium import webdriver

driver = webdriver.Chrome()          # Selenium Manager gets the driver
try:
    driver.get("https://example.com")
    print(driver.title)              # the page's <title>
    print(driver.current_url)
finally:
    driver.quit()                    # always close the session

webdriver.Chrome() starts the driver and a Chrome window, creating a session as you saw in How WebDriver Works. Use webdriver.Firefox() or webdriver.Edge() for the other browsers; the rest of the code does not change.

Java

Add the dependency to your Maven pom.xml (use the current version listed on the Selenium site in place of the placeholder):

XML
<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>VERSION</version>
</dependency>

Then:

Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FirstScript {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com");
            System.out.println(driver.getTitle());
            System.out.println(driver.getCurrentUrl());
        } finally {
            driver.quit();
        }
    }
}

Note that Java declares the variable as the WebDriver interface. That lets you swap ChromeDriver for FirefoxDriver in one place.

JavaScript

Install the package with npm:

Terminal
npm install selenium-webdriver
JavaScript
const { Builder } = require('selenium-webdriver');

(async function firstScript() {
  const driver = await new Builder().forBrowser('chrome').build();
  try {
    await driver.get('https://example.com');
    console.log(await driver.getTitle());
    console.log(await driver.getCurrentUrl());
  } finally {
    await driver.quit();
  }
})();

In JavaScript every driver call returns a promise, so each one is awaited. Python and Java calls simply block until the driver replies.

The anatomy of the script

All three versions do the same four things.

  1. Start. Create the driver object. A browser window appears.
  2. Navigate. get loads a URL and, by default, returns when the page's load event has fired.
  3. Read. title and current_url ask the browser what it is showing now.
  4. Quit. quit() ends the session and closes every window the session opened.

Two related calls are easy to mix up:

Call What it does
driver.close() Closes the current window or tab only. If it was the last one, the session may still be alive.
driver.quit() Ends the whole session: closes all windows and stops the driver process.

Use quit() at the end of a script or test. The try and finally blocks make sure it runs even when something throws. Test frameworks do this with setup and teardown hooks, which you will see in Selenium with pytest.

If a script crashes before quit(), the browser and driver processes can keep running in the background and use memory. Always put quit() in a finally block or a test teardown.

A stand-in: a driver object

This is plain JavaScript, not Selenium. It models what the library gives you: an object with get, title, current_url, close and quit, where navigation to a page changes what the object reports, and calling anything after quit fails because the session is gone.

JS A fake driver object and its lifecycle

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last lines show why quit() matters: after the session ends, any further call fails. A real driver reports this as an invalid session error.

The cost of forgetting to quit

Each real session is a browser process plus a driver process. This second stand-in counts open processes to show how forgetting quit() adds up across a suite of tests.

JS Leaked sessions across a test run

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Four tests with no cleanup leave eight processes behind. With a hundred tests the problem becomes a very real one, which is why fixtures and teardown matter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Selenium Manager do?

    Selenium Manager is built into current Selenium libraries and handles driver setup, so you rarely download drivers by hand.
  2. 2. What is the difference between close() and quit()?

    close closes the focused window or tab. quit ends the session, closes all windows and stops the driver process.
  3. 3. Why is quit() placed in a finally block or test teardown?

    finally runs whether the code succeeded or threw. Without it, a failure can leave browser and driver processes running.
  4. 4. In the JavaScript binding, why is every driver call awaited?

    The JavaScript binding is asynchronous, so each call returns a promise that must be awaited before you use the result.

Summary

  • Install the Selenium binding for your language; Selenium Manager fetches the matching driver.
  • The first script starts a driver, navigates with get, reads title or current_url, and quits.
  • close() closes one window; quit() ends the whole session.
  • Put quit() in finally or a test teardown so failures do not leak processes.
  • The fake driver objects above are stand-ins; the real code needs a browser and is read-only here.

Next, learn to find things on the page in Locators: id, name, class and tag.