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
- Send the driver to the Text Fields page with
driver.get(), using the full address from the task. - Ask the driver for the page title.
getTitle()hands it back as aString. - Print one line: the label
Title:, a space, then the title. - 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. Putquit()in afinallyblock 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 throwsNoSuchSessionException, yet the chromedriver process keeps running untilquit(). With other windows still open, the next call throwsNoSuchWindowExceptionuntil you switch to one of them.Reading the title before any navigation
A new Chrome session starts on a blank
data:,page, sogetTitle()returns an empty string until you callget(). 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.
Related lessons
- Navigate Between PagesLoad a second page in the same session and compare
get()withnavigate().to(). - Get Window Handle and TitleEvery session you start owns at least one window, and each window has a handle.
- Window Size and PositionSize the browser window before a test so layouts behave the same on every run.
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.
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();
}
}