When working with web automation in Python, knowing how to get the current URL is essential. It helps you confirm where the browser is, catch unexpected redirects, and make sure your test steps behave as expected.
The command driver.get("url") is what takes you to a page. But after that, how do you check if you're on the right one? That’s where driver.current_url comes in. It lets you ask the browser, “Where am I now?”
In this tutorial, we'll walk through everything you need to know to use driver.current_url effectively in Selenium with Python. Here’s what you’ll learn:
Let’s get started!
You can get the current page’s URL using a simple and clear workflow. It works right after you open a page using Selenium.
Here’s how you do it:
This is useful when you want to confirm that your test has landed on the right page. For example, after clicking a login button or navigating to a product detail page.
The code below shows a working example.
from selenium import webdriver
# Launch the browser
driver = webdriver.Chrome()
# Navigate to the website
driver.get("https://katalon.com")
# Get the current URL
current_url = driver.current_url
# Print the URL
print("Current URL:", current_url)
driver.quit()
This script launches Chrome, opens Katalon’s homepage using driver.get(url), then prints out the actual address loaded in the browser.
💡 Insight: Use driver.current_url right after page navigation to confirm redirection, especially during login or form submissions.
You can use the get current URL method to check redirection after logging in. It also confirms that a button click or link navigation leads to the intended page. If you are testing a checkout process, this check ensures the user ends up on the right step in the flow.
Here are some ways teams use driver.current_url in automated tests:
The following script shows how to verify the current URL after navigation using driver.get(url).
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://katalon.com/login")
# Assume login happens here
expected_url = "https://katalon.com/dashboard"
assert expected_url in driver.current_url
driver.quit()
This check confirms that the page redirection worked as intended after login. You can repeat this pattern for any page transition that matters to your test case.
💡 Insight: Always place your URL check right after the navigation step triggered by driver.get(url) or a UI interaction. This gives fast, reliable feedback on flow accuracy.
Both driver.get(url) and driver.current_url play key roles in navigation and validation. They work together in most test flows, but they serve different purposes.
Use driver.get(url) to open a page. Use driver.current_url to check where the browser is at any point during the test.
Here’s a quick comparison to help you understand how they differ.
Property | driver.get(url) | driver.current_url |
---|---|---|
Action | Navigates to a new webpage | Returns the current page’s URL |
Return type | None (performs navigation only) | String (the full URL of the page) |
Common use | Start or reset the test flow | Verify that navigation completed as expected |
Timing | Executed before interaction begins |
Used after interactions or navigation |
💡 Insight: Think of driver.get(url) as the start of the journey and driver.current_url as your GPS check along the way. Both are essential for stable and trackable test scripts.
Every test has a goal. Sometimes it’s to log in. Sometimes it’s to complete a transaction or move through a workflow. Using driver.current_url helps confirm that every step reaches the intended page.
Below are real-world situations where checking the current URL keeps your test flow clean and predictable.
Insight: Before troubleshooting UI elements, always use driver.current_url to confirm page context. It saves time and narrows down the issue quickly.
Let’s look at how driver.current_url fits into real test flows. Each of these examples shows a different use case that QA teams handle every day. They use driver.get(url) to open pages, and then check the browser’s actual address using driver.current_url.
Here are three examples using Selenium with Python.
Example 1: After login, check dashboard URL
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://katalon.com/login")
# Simulate login steps here
# driver.find_element(By.ID, "username").send_keys("user")
# driver.find_element(By.ID, "password").send_keys("pass")
# driver.find_element(By.ID, "login-button").click()
expected_url = "https://katalon.com/dashboard"
assert expected_url in driver.current_url
driver.quit()
Example 2: After clicking a link, verify destination
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://katalon.com")
# Click a link that goes to the Resources page
driver.find_element(By.LINK_TEXT, "Resources").click()
assert "resources" in driver.current_url
driver.quit()
Example 3: Loop through multiple links and verify each URL
from selenium import webdriver
driver = webdriver.Chrome()
urls_to_test = [
"https://katalon.com/platform",
"https://katalon.com/pricing",
"https://katalon.com/resources-center/blog"
]
for url in urls_to_test:
driver.get(url)
assert url in driver.current_url
print("Verified:", driver.current_url)
driver.quit()
Each example above follows the same reliable pattern. First, use driver.get(url) to reach a page. Then use driver.current_url to confirm it. This gives your tests structure and clarity at every step.
💡 Insight: You can use this method in any situation that requires step-by-step navigation checks across pages.
Using driver.current_url works best when the browser has finished loading the page. If the script moves too quickly, it might fetch the URL before the transition completes. This affects the accuracy of your checks.
Here are a few common issues you may encounter and how to solve them.
The best way to solve these issues is to apply a wait condition before checking the current URL. This tells the test to pause until the change completes.
Fix: Use WebDriverWait with url_to_be or url_changes to wait for navigation to finish before calling driver.current_url.
💡 Insight: Add your URL check only after you're confident the browser has landed on the new page. This improves test stability across all flows that use driver.get(url) or element clicks.
In fast test flows, the browser may take a few seconds to complete navigation. To make sure you only check the URL when it’s ready, you can wait until it changes using expected conditions.
The method url_changes watches the current address and pauses the test until it becomes different from the original value. This works well after clicking buttons, submitting forms, or calling driver.get(url).
Here is a simple example using Python and Selenium.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://katalon.com")
old_url = driver.current_url
# Simulate a page action here that causes a URL change
WebDriverWait(driver, 10).until(EC.url_changes(old_url))
print("New URL:", driver.current_url)
driver.quit()
This wait keeps the test from rushing ahead before the browser updates. It creates more stable checks and removes uncertainty from timing delays.
💡 Insight: Use this pattern when you expect a redirect or URL change after user interaction. It improves accuracy, especially in login and checkout tests.
Katalon is a modern low-code test automation platform. It is built on top of Selenium, with additional features that support fast test development, easy maintenance, and scalable execution. While Selenium gives you the raw engine, Katalon wraps it into a platform that works out of the box for both beginners and experienced testers.
With Katalon, you get:
In short, Katalon makes Selenium more productive. It gives you a complete testing environment with less setup and more control. You can test faster, collaborate better, and scale smoothly with your growing application.
📝 Want to explore what Katalon can do for your team? Request a demo to see how it helps teams automate faster with less effort.