Skip to content
CodeKruPro

Your First Selenium Script

1/70

Video walkthrough

Watch on YouTube

Your First Selenium Script

Welcome to Selenium! In this first assignment, you will:

  1. Create a ChromeDriver instance
  2. Navigate to a webpage using driver.get()
  3. Get the page title using driver.getTitle()
  4. Close the browser with driver.quit()

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Print the page title

Expected output:

Title: Text Fields
Read full tutorial on Codekru

About this lesson

Your first Selenium WebDriver script in Java: open Chrome and read the page title

Learn Selenium JavaBeginnerGetting Started

How it works

Every Selenium Java test talks to the browser through the WebDriver interface. ChromeDriver is the class that implements it for Chrome, so you write WebDriver driver = new ChromeDriver(); and keep the variable typed as the interface. Code written against WebDriver works unchanged if you later swap in FirefoxDriver or EdgeDriver.

Creating the object does real work. Since Selenium 4.6, Selenium Manager finds a matching chromedriver binary on its own, starts it as a separate process and opens a new browser session. driver.get() then loads a URL and waits for the page to finish loading, and driver.quit() ends the session and stops that background process.

Reading the title is the smallest useful check you can make against a live page: it proves the browser started, the address resolved and the right document loaded, all before you locate a single element.

About the assignment

The starter gives you a working driver and the quit() call already in place. Your job sits between them: load the Text Fields practice page and report the title the browser shows, so you see one full start-to-finish WebDriver session.

What you will do

  1. Send the driver to the Text Fields page with driver.get(), using the full address from the task.
  2. Ask the driver for the page title. getTitle() hands it back as a String.
  3. Print one line: the label Title:, a space, then the title.
  4. Leave driver.quit() as the last line so Chrome closes when the program ends.

Common mistakes

  • Skipping quit() when an exception is thrown

    If a line fails before driver.quit() runs, the chromedriver process keeps running in the background. A long test run can leave dozens of them behind. Put quit() in a finally block or in a teardown method.

  • Using close() to end the test

    close() only shuts the current window. Here that is the last window, so the session ends and any later call throws NoSuchSessionException, yet the chromedriver process keeps running until quit(). With other windows still open, the next call throws NoSuchWindowException until you switch to one of them.

  • Reading the title before any navigation

    A new Chrome session starts on a blank data:, page, so getTitle() returns an empty string until you call get(). No exception warns you; the print simply shows nothing after the label.

In a real test suite

In a JUnit 5 or TestNG suite you rarely call new ChromeDriver() inside a test. The driver is created in a @BeforeEach method and quit in @AfterEach, and a title check like this one is a common first smoke test that the application is up.

The next lesson keeps the same session alive across two different pages, and the one after it walks back and forward through the history those page loads create.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        
        // TODO: Navigate to https://testkru.com/Elements/TextFields
        // TODO: Print the page title in format: "Title: <title>"
        
        driver.quit();
    }
}
Read the full tutorial