skip to content

Layout and Navigation

How large the window is and where the driver has steered it: the two settings that quietly decide which layout and which page a test actually sees before a single locator runs.

on this pageshow

explore

questions

7

In Selenium, what is the difference between driver.get(url) and driver.navigate().to(url)?

level: juniorimportance: must knowfreq 78%

answer

  1. Two spellings, one browser action
  2. One is documented as an alias
  3. Think about back and forward
  4. Same POST to /session/{id}/url

basics

~10 s

Nothing functional separates them. Both send the same WebDriver Navigate To command and load the URL in the current tab. navigate() exists because the object it returns also carries back, forward and refresh.

solid answer

~40 s

In Selenium 4 they are the same command. `driver.get(url)` and `driver.navigate().to(url)` both serialise to the W3C **Navigate To** request, `POST /session/{sessionId}/url`, so the driver cannot tell them apart; Selenium's docs describe `get()` as an alias for `navigate().to()`. The difference is what each one can reach. `get(String)` is a single method on `WebDriver`, while `navigate()` returns a `WebDriver.Navigation` object that also declares `back()`, `forward()`, `refresh()` and a `to(java.net.URL)` overload. So a claim-intake test that only opens the intake page uses `get()`; one that steps back to correct the policy number needs `navigate()`. Both replace the document in the current tab rather than opening a new one, and both demand an absolute URL — a relative path such as `/intake` raises `InvalidArgumentException`.

code

java · 20 lines
java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClaimIntakeNavigation {

    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://claims.example.test/intake");
            System.out.println(driver.getTitle());

            driver.navigate().to("https://claims.example.test/intake/step-2");
            driver.navigate().back();

            System.out.println(driver.getCurrentUrl());
        } finally {
            driver.quit();
        }
    }
}

go deeper

for a junior

Be ready to say the two calls are equivalent, and to name what navigate() adds: back, forward and refresh. Knowing that the URL must be absolute is expected at this stage.

for a middle

Explain that both serialise to the same W3C Navigate To request, so the remote end cannot distinguish them, and that navigate() returns a Navigation object which is where the history methods are declared.

for a senior

An interviewer expects you to say why the choice rarely matters in a real suite, then steer to what does cause navigation trouble: asserting on a URL that redirected, and reloading a page produced by a form submission.

for a principal

Own the convention so reviewers stop debating it: one spelling for opening pages across the suite, and base-URL composition kept in a single configuration seam rather than scattered through string concatenation.

## Two names for one W3C command In **Selenium 4** the Java binding turns both `driver.get(url)` and `driver.navigate().to(url)` into exactly the same request to the remote end: the W3C WebDriver **Navigate To** command, `POST /session/{sessionId}/url`, with the target address in the body. ChromeDriver, GeckoDriver or EdgeDriver receives one identical message either way and has no way of knowing which Java method your test called. Selenium's own documentation states the relationship plainly: `get()` is a convenience alias for `navigate().to()`. For an insurance-claim intake suite, these two lines are interchangeable: - `driver.get("https://claims.example.test/intake")` - `driver.navigate().to("https://claims.example.test/intake")` Both replace the document in the **current top-level browsing context** — the tab the session is focused on right now. Neither opens a new tab, neither starts a second session, and neither is measurably faster than the other, because there is exactly one command underneath both spellings. ## What `navigate()` hands you that `get()` cannot The difference is reach, not behaviour. `get(String)` is a single method declared on the `WebDriver` interface. `navigate()` is a different kind of thing: it returns a `WebDriver.Navigation` object that represents the navigation surface of that tab, and that object declares five methods: 1. `to(String url)` — load a URL; the identical request `get` sends. 2. `to(java.net.URL url)` — the same command from a typed URL, and the only API-level difference from `get`. 3. `back()` — the W3C **Back** command, `POST /session/{sessionId}/back`. 4. `forward()` — the W3C **Forward** command. 5. `refresh()` — the W3C **Refresh** command, which reloads the current document. That is why the distinction matters in practice. A test that fills the claim intake form, moves on to the review step and then has to step back to correct the policy number can only reach `back()` through `navigate()`; `get()` cannot express it. Conversely, `get()` is the shorthand for the case that dominates a suite — open a page and start work. | | `driver.get(url)` | `driver.navigate().to(url)` | |---|---|---| | Command sent | Navigate To | Navigate To | | Declared on | `WebDriver` | `WebDriver.Navigation` | | Accepts a `java.net.URL` | No | Yes | | Reaches `back`, `forward`, `refresh` | No | Yes, from the same object | | Opens a new tab | No | No | ## The URL has to be absolute Navigate To takes an absolute URL. The driver holds no base URL and no working directory, so nothing resolves `"/intake"` on your behalf: the remote end rejects the argument and the Java binding raises `InvalidArgumentException`. Compose the full address in the harness — a configured base string plus the path — before it reaches Selenium. This is the one input mistake that turns a "wrong page" bug into an immediate, clearly named exception instead of a puzzling failure three steps later. ## Reading back where you landed Two paired reads answer the question "did I actually get there": - `driver.getCurrentUrl()` sends `GET /session/{sessionId}/url` and returns the address of the document the tab is showing **now**, after any redirects the server performed — not the string you passed in. - `driver.getTitle()` sends `GET /session/{sessionId}/title` and returns the title of that same document. If submitting the intake form lands the tab on `/claims/CLM-8842/confirmation`, `getCurrentUrl()` reports that final address, so an assertion comparing it with the URL you originally requested fails even though the navigation worked perfectly. ## What neither call promises - Neither method returns a value you can assert on; the assertion is a separate read afterwards. - Neither guarantees the field you are about to type into exists yet — waiting for content is a different mechanism with its own API. - How much of the page load the call waits for before returning is governed by the session's page-load configuration, which is set up elsewhere and is not a property of `get` versus `navigate`. - Neither clears cookies, storage or anything else in the browser profile; the new document inherits whatever the session already carried. ## Which one to write Use `get()` for the plain "open this page" step: it is shorter and reads as the intent. Reach for `navigate()` when the same block of code also walks history, or when you already hold a `java.net.URL` rather than a string. Treat any claim that one of them is faster, or that one "waits properly" and the other does not, as a misconception to correct — the wire traffic is identical, and an interviewer asking this question is usually checking whether you know that.

  • Does driver.get() ever open a new tab or window?
    No. Navigate To always replaces the document in the current top-level browsing context, so the tab count is unchanged. Opening a tab is a different command, and once a second tab exists the session still has to be switched into it before any navigation call applies there.
  • What does driver.get() return, and how do you assert that the navigation worked?
    It returns void, so the assertion is a separate read afterwards: `getCurrentUrl()`, `getTitle()`, or locating an element that exists only on the target page. Comparing `getCurrentUrl()` with the string you passed in is fragile, because redirects, generated ids and URL normalisation all change it.
  • Why is a relative URL rejected rather than resolved against the current page?
    Because the Navigate To command takes an absolute URL and the driver holds no base URL to resolve against. The remote end answers with an invalid argument error, which the Java binding surfaces as `InvalidArgumentException`. Harnesses supply the base address themselves, normally from configuration.

saying these in an interview costs you the question

  • Claiming navigate().to is slower or heavier than get
  • Saying get opens a new tab while navigate reuses the current one
  • Believing get waits for the load but navigate().to does not
  • Passing a relative path like /intake and expecting the driver to resolve it
  • Thinking back and forward are declared directly on the driver
open as a page

In Selenium, why can navigate().refresh() after submitting an insurance-claim form create a duplicate claim?

level: seniorimportance: must knowfreq 52%

basics

~10 s

A reload repeats the request that produced the current document. If the confirmation page was rendered straight from the form's POST, refreshing sends that POST again and the server records a second claim.

open as a page

In Selenium's Java client, how do you set and read the browser window's size and position?

level: juniorimportance: should knowfreq 63%

basics

~10 s

Go through driver.manage().window(). Call setSize with a Dimension of width and height, and setPosition with a Point of x and y. Read them back with getSize and getPosition, which return the same two types.

open as a page

In Selenium, why does a headless run render a responsive page at a different layout than your headed run?

level: middleimportance: should knowfreq 58%

basics

~20 s

A headless browser has no desktop to size a window from, so it opens at a small fixed default - 800 by 600 in Chrome - and the page picks its narrow layout. Set the width explicitly at launch.

open as a page

In Selenium, what is the difference between manage().window().maximize(), fullscreen() and minimize()?

level: juniorimportance: nice to knowfreq 36%

basics

~20 s

Maximize grows the window to fill the desktop's working area with the browser toolbars still showing. Fullscreen puts it in the browser's full-screen mode, hiding that chrome. Minimize hides the window from the display entirely.

open as a page

In Selenium, what does driver.getCurrentUrl() return after a submitted claim form redirects to a confirmation page?

level: middleimportance: nice to knowfreq 41%

basics

~20 s

The address the tab is showing now, after every redirect has been followed, not the URL your test passed in. It is a live read from the browser, taken at the moment the call executes.

open as a page

A Selenium suite sets the window to 1280 wide, yet a min-width 1280px media query never matches. Why?

level: seniorimportance: nice to knowfreq 41%

basics

~20 s

Selenium sizes the whole browser window, not the page. Browser chrome and the vertical scrollbar come out of that number, so a 1280-wide window lays the page out in slightly less than 1280 CSS pixels and the query misses.

open as a page