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
- Load Text Fields with
driver.get()and print its title after the labelPage 1:. - Load Buttons, this time with
driver.navigate().to(), so you try both styles. - Print the second title after the label
Page 2:. - 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 withInvalidArgumentException: invalid argument; always pass the full address, includinghttps://.Reusing an element from the previous page
A
WebElementfound before you navigate points at a page that no longer exists. Using it afterwards throwsStaleElementReferenceException, 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.
Related lessons
- Your First Selenium ScriptThe one-page version of this session, with the driver lifecycle explained.
- Browser Back and Forward NavigationWalk the history these two loads create with
back()andforward(). - Handle Multiple WindowsOpen the second page in a new tab instead of replacing the first one.
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 TextFields page and print title
// TODO: Navigate to Buttons page and print title
driver.quit();
}
}