In Selenium, what does driver.getCurrentUrl() return after a submitted claim form redirects to a confirmation page?
answer
- Not a memory of what you requested
- The address after every redirect
- A live read at that instant
- Top-level document, never the frame
basics
~20 sThe 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.
solid answer
~40 s`driver.getCurrentUrl()` sends the W3C **Get Current URL** command and returns the document URL of the tab's top-level browsing context as it stands at that instant. If submitting the claim form redirects to `/claims/CLM-8842/confirmation`, that redirected address is what comes back, generated claim reference and all; the URL you handed to `get()` is not remembered anywhere. `getTitle()` is its companion, returning the same top-level document's title. Both are scoped to the top-level context, so switching the session into a frame does not change what they report. Neither waits for anything — they read whatever is current when called — which is why exact equality against a requested URL is fragile, while a check on a stable path segment such as `/confirmation` holds up.
go deeper
Know that getCurrentUrl asks the browser for the address it is showing right now, and that after a redirect this is not the URL you passed to get. Use it to check where a step actually landed.
Explain that the call maps to the Get Current URL command and reads the top-level document's address at that instant, which is why redirects, generated identifiers and URL normalisation all make exact-equality assertions fragile.
Show judgement about what a URL assertion is worth: assert a stable path segment, capture the confirmation address for later reuse, and prefer a content check wherever one address can serve several application states.
Decide what URLs mean as a contract for the suite — which path shapes tests may depend on, and whether the application owes them stability — so a routing change becomes a deliberate conversation rather than a morning of red tests.
## What the call actually reads `driver.getCurrentUrl()` is not a memory of what your test asked for. It sends the W3C WebDriver **Get Current URL** command, `GET /session/{sessionId}/url`, and the remote end answers with the serialised document URL of the **top-level browsing context**'s active document — the address the tab is displaying at that instant. Selenium keeps no record of the string you handed to `get()` or `navigate().to()`, so there is nothing to compare against except the live state of the browser. For a claim intake suite that matters immediately, because the submit step almost never leaves the tab where it started. ## Why it differs from the URL you requested Several ordinary things change the address between the request and the read: - **Server redirects.** `POST /claims` answers with a redirect to `/claims/CLM-8842/confirmation`; the tab follows it, and `getCurrentUrl()` reports the destination rather than the form's action. - **Generated identifiers.** The claim reference in that path did not exist when the test started, so no literal written into the test can ever match it. - **Appended query parameters.** A locale, campaign or tracking parameter the server adds on the way comes back in the string too. - **URL normalisation.** Ask for `https://claims.example.test` and you get `https://claims.example.test/` back — the empty path is serialised as a slash. - **Later navigation.** If the page moves the address on after loading, a later read returns the later address; the value is only true for the moment it was taken. | What the test passed | What `getCurrentUrl()` returns afterwards | |---|---| | `https://claims.example.test` | `https://claims.example.test/` | | `https://claims.example.test/intake`, then a redirect | `https://claims.example.test/intake/step-1` | | The intake form's action, submitted | `https://claims.example.test/claims/CLM-8842/confirmation` | ## `getTitle()`, the companion read `driver.getTitle()` sends the **Get Title** command, `GET /session/{sessionId}/title`, and returns the document title of the same top-level context — the value the page's `<title>` element supplies. Two practical notes: - It reads the title as the browser currently computes it, so a page that rewrites its title after loading can return different values at different moments in the same test. - A title is application copy. It changes for reasons that have nothing to do with navigation — a marketing edit, a translation, a trailing product name — which makes it a weaker landing assertion than a path segment. ## Both are scoped to the top-level context The WebDriver protocol defines both commands against the **current top-level browsing context**, not against whatever the session happens to be switched into. After the session switches into an iframe embedded in the intake page, `getCurrentUrl()` still reports the page's own address and `getTitle()` still reports the page's own title; neither tells you anything about the frame's document. Reading a frame's own source address needs a different route entirely. ## Assertion patterns that hold up 1. **Assert on a stable segment, not the whole string.** Check that the current URL contains `/confirmation`, or that it matches a pattern such as `/claims/[A-Z]{3}-\d+/confirmation`, instead of demanding equality with a literal. 2. **Capture, then reuse.** Read `getCurrentUrl()` once after the submit and keep it: that captured address is how you return to the confirmation later with `navigate().to(...)`, and in some applications it is the only place the claim reference appears in machine-readable form. 3. **Assert on content when the URL is not the contract.** If one address can serve several states — a wizard that keeps `/intake` across every step — an element on the page is the stronger check and the URL is a weak proxy for it. ## A point-in-time read, not a wait Neither call blocks. `getCurrentUrl()` returns whatever is current the moment it executes, so calling it too soon after clicking Submit can return the intake form's address rather than the confirmation's, and the assertion then fails on timing rather than on behaviour. The remedy is Selenium's waiting API, which is a separate mechanism with its own topic; the point here is that these two reads carry no waiting of their own and are never a substitute for one. ## Where the pair earns its place in a claim intake test - Straight after `get()`, to prove the suite is pointed at the environment it thinks it is pointed at. - After a submit, to capture the confirmation's address so a later step can revisit it. - After `navigate().back()`, to prove the tab really moved rather than silently staying put. - In a failure message, where "expected /confirmation but was /intake" says far more to the next reader than "element not found".
- How would you assert the confirmation URL when it contains a generated claim reference?Match a shape rather than a literal: assert the current URL matches something like `/claims/[A-Z]{3}-\d+/confirmation`, or that it contains both `/confirmation` and the reference the page displayed. Asserting that the URL carries the reference shown on screen also proves the two agree.
- Does getCurrentUrl() change what it reports once the session switches into a frame?No. The command is defined against the current top-level browsing context, so it keeps reporting the page's own address whichever frame the session is switched into. `getTitle()` behaves the same way. Neither is a route to a frame's own source address.
- Why can getCurrentUrl() return the previous page's address right after a click?Because it is a point-in-time read with no waiting built in. If the click's navigation has not committed yet, the tab still holds the old document and the old address comes back. Synchronise with Selenium's waiting API rather than reading twice and hoping.
saying these in an interview costs you the question
- Expecting getCurrentUrl to echo the URL passed to get
- Asserting exact equality against a URL holding a generated id
- Believing getTitle reports the currently selected frame's title
- Treating getCurrentUrl as a wait for the next page
- Reading the address once and assuming it stays valid