Skip to content
CodeKruPro

Navigate Between Pages

2/70

Video walkthrough

Watch on YouTube

Navigate Between Pages

You can navigate to multiple pages in a single script. Selenium provides two ways:

  • driver.get(url) - navigates to a URL
  • driver.navigate().to(url) - also navigates to a URL

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Print the title
  3. Navigate to https://testkru.com/Elements/Buttons
  4. Print the title

Expected output:

Page 1: Text Fields
Page 2: Buttons

About this lesson

driver.get vs navigate().to in Selenium Java: loading two pages in one session

Learn Selenium JavaBeginnerGetting Started

How it works

Selenium Java gives you two ways to load a URL: driver.get(url) and driver.navigate().to(url). They behave the same way. Both block until the browser reports the page as loaded, which with the default page load strategy means document.readyState is complete.

The difference is where they live. navigate() returns a WebDriver.Navigation object that also holds back(), forward() and refresh(), so some teams use navigate().to() to keep every history action in one style. get() is shorter and is what most examples use.

About the assignment

This assignment strings two page loads together in one session. You visit Text Fields first and Buttons second, printing each title right after its page loads, which shows that one driver can move around a whole site.

What you will do

  1. Load Text Fields with driver.get() and print its title after the label Page 1:.
  2. Load Buttons, this time with driver.navigate().to(), so you try both styles.
  3. Print the second title after the label Page 2:.
  4. Keep the prints in that order; each title must be read after its own page has loaded.

Common mistakes

  • Passing a relative path

    Java WebDriver has no base URL. driver.get("/Elements/Buttons") fails with InvalidArgumentException: invalid argument; always pass the full address, including https://.

  • Reusing an element from the previous page

    A WebElement found before you navigate points at a page that no longer exists. Using it afterwards throws StaleElementReferenceException, so find elements again after every navigation.

  • Assuming get() waits for everything

    It waits for the page load event, not for data that JavaScript fetches later. Content added after load still needs an explicit wait, which the waits lessons cover from lesson 35 on.

In a real test suite

Real suites keep the site address in one constant or configuration value, such as BASE_URL + "/login", and page object classes expose methods like open() so tests never repeat raw URLs.

Every page you load here also lands in the tab's history list. Lesson 3 uses exactly that list to step back to Text Fields and forward to Buttons again without typing either address.

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 TextFields page and print title
        // TODO: Navigate to Buttons page and print title
        
        driver.quit();
    }
}