All All News Products Insights AI DevOps and CI/CD Community

Get current URL in Selenium using Python: A complete tutorial

Discover how to get the current URL in Selenium WebDriver using Python with this complete tutorial.

Hero Banner
Blog / Insights /
Get current URL in Selenium using Python: A complete tutorial

Get current URL in Selenium using Python: A complete tutorial

QA Consultant Updated on

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:

  • How to retrieve the current page’s URL after calling driver.get(url)
  • How to use the current URL to verify navigation or redirection
  • Common mistakes when checking URLs and how to fix them
  • Practical examples to check login success, redirections, and link accuracy
  • When and how to wait for URL changes using explicit waits

Let’s get started!

How to get current URL in Selenium WebDriver using Python? (with example)

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:

  • Start the browser using Selenium WebDriver
  • Use driver.get("url") to load the page
  • Call driver.current_url to retrieve the current page’s address
  • Print the URL or compare it with your expected value

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.

Python
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.

banner9 (6)-1

How to perform URL checks with current URL in Selenium using Python?

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:

  • Login flow: After clicking the login button, verify that the dashboard URL loads
  • Link validation: After clicking a “Contact” or “Help” link, confirm the right page opens
  • Checkout checks: Confirm the user reaches the payment page after reviewing the cart

The following script shows how to verify the current URL after navigation using driver.get(url).

Python
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.

Difference between .current_url vs .get()

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.

Practical use cases of getting current URL

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.

  • Validate login success: After submitting login credentials, check that the browser loads the correct dashboard page
  • Track redirects in checkout: After reviewing the cart, confirm that the payment page opens by comparing the current URL to the expected one
  • Confirm form submission flow: After submitting a form, use driver.current_url to ensure it leads to the success or thank-you page
  • Pinpoint loading issues: When a page fails to show expected content, check the URL first to make sure it landed on the correct page

Insight: Before troubleshooting UI elements, always use driver.current_url to confirm page context. It saves time and narrows down the issue quickly.

Examples: current URL in different scenarios

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

Python
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

Python
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

Python
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.

Common issues when using .current_url in Selenium

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.

  • Old URL appears: The browser shows the previous address because it hasn’t finished navigating. Fix this by waiting for the URL to change using explicit wait conditions
  • Empty or partial URL: When the page is still loading, the current URL may return incomplete information. Use timing control to ensure the page is fully loaded before checking
  • Unstable results in long flows: On pages with large scripts or animations, the final URL may take a few extra seconds to load. Use wait conditions to keep the test steady

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.

Advanced: waiting for URL changes in Selenium Python

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.

Python
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.

Why choose Katalon to automate tests?

Katalon-as-top-automation-testing-tool

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:

  • Unified platform: bring together test design, execution, management, and reporting in one solution
  • Cross-browser and cross-platform: run tests across thousands of browser and OS combinations without setting up drivers manually
  • Scalability: execute tests in parallel and integrate easily into CI/CD pipelines and cloud environments
  • Smart test maintenance: AI-powered self-healing locators reduce flakiness when UI elements change during development
  • Built-in test management and analytics: dashboards and reports show trends, coverage, and test performance clearly

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.

banner8 (1

📝 Want to explore what Katalon can do for your team? Request a demo to see how it helps teams automate faster with less effort.

Explain
|
Vincent N.
Vincent N.
QA Consultant
Vincent Nguyen is a QA consultant with in-depth domain knowledge in QA, software testing, and DevOps. He has 10+ years of experience in crafting content that resonate with techies at all levels. His interests span from writing, technology, building cool stuff, to music.
on this page
Click