How do I get current URL in Selenium Webdriver 2 Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When a Selenium test triggers navigation, one of the simplest diagnostics is the browser's current address. Python WebDriver exposes that value directly, but reliable assertions require more than printing the URL once and hoping the redirect already finished.
Read the Current URL
The property you want is driver.current_url. It returns the address for the active browser tab or window. Because it is a property, not a method, you use it without parentheses.
Here is a minimal example:
This is enough for debugging and for simple smoke tests. The main thing to remember is that the value reflects the page after navigation completes, not necessarily the URL you originally passed into get.
Wait for Redirects Before Asserting
The most common mistake is checking the value too soon. Login flows, JavaScript redirects, and client-side routing can take time. If you assert immediately after a click, the test may read the old URL and fail intermittently.
Use WebDriverWait with Selenium's URL conditions:
This is more stable than time.sleep(2) because it waits only as long as needed. It also makes your test intent explicit: you are waiting for navigation, not inserting an arbitrary pause.
If you know the final address exactly, use EC.url_to_be:
Use exact matches only when the application does not append dynamic query parameters or fragments.
Validate Path, Query, and Fragments Separately
Real applications often add paging, tracking, or state parameters. In those cases, parsing the URL is clearer than comparing one long string.
You can use the same approach with driver.current_url:
This keeps the test focused on what matters. A query string can change order without changing application behavior, so comparing the parsed pieces is usually more robust.
Use URL Checks Alongside Page Assertions
A URL assertion alone rarely proves the page is ready. Single-page apps can change the address before content has finished rendering, and some workflows reuse the same path while replacing large parts of the DOM.
Combining both checks gives you a stronger end-to-end assertion: navigation happened, and the destination screen is actually usable.
New Tabs And Window Focus
driver.current_url always belongs to the currently selected window handle. If clicking a link opens a new tab, switch to it before checking the address:
Without the switch, you would still be reading the URL from the original tab and the assertion would be misleading.
Common Pitfalls
- Calling
driver.current_url()with parentheses. It is a property, so usedriver.current_url. - Reading the value immediately after a click. Wait for the redirect or route change first.
- Comparing raw strings when the server normalizes the address. Trailing slashes, lowercase hosts, and encoded query values can change.
- Forgetting about popups or new tabs.
current_urlalways belongs to the active window handle. - Treating the URL as the only source of truth. Confirm an element on the page as well when the flow matters.
Summary
- Use
driver.current_urlto read the active browser address in Selenium with Python. - Prefer
WebDriverWaitover fixed sleeps when navigation happens asynchronously. - Parse dynamic URLs with
urlparsewhen only part of the address matters. - Combine URL assertions with element-based checks for stronger end-to-end tests.

