Introduction
The current_url property is one of the most useful browser properties in Selenium WebDriver. It is used to retrieve the URL of the currently loaded web page.
Whenever Selenium navigates to a web page, the browser displays its URL in the address bar. The current_url property allows automation scripts to retrieve this URL and verify that the browser has navigated to the expected page.
The current_url property is widely used in automation testing for validating page navigation, login success, redirects, URL parameters, and ensuring users land on the correct page after performing an action.
In this tutorial, you will learn what the current_url property is, why it is used, its syntax, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.
What is current_url?
The current_url property returns the complete URL of the currently loaded web page.
Unlike browser commands such as close() or quit(), the current_url property does not perform any browser action. Instead, it retrieves information about the page currently displayed in the browser.
Why Do We Use current_url?
The current_url property is commonly used to:
Verify successful page navigation.
Validate page redirections.
Confirm login success.
Verify logout functionality.
Check URL parameters.
Validate routing in web applications.
Perform assertions during automation testing.
Syntax
driver.current_url
The current_url property does not accept any parameters.
Practical Example
The following example demonstrates how to retrieve and validate the URL of the currently loaded web page.
The automation script opens the Selenium practice login page and verifies that the current URL ends with /login.
from selenium import webdriver
# Topic: 5. Browser Commands - current_url
# Practice site: https://the-internet.herokuapp.com/login
#
# driver.current_url returns the full URL of the page currently loaded.
def test_get_current_url():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/login")
assert driver.current_url.endswith("/login")
finally:
driver.quit()
Output
Browser launched successfully.
Website opened successfully.
Current URL:
https://the-internet.herokuapp.com/login
Assertion Passed.
Test Executed Successfully.
Understanding the Code
Import WebDriver
from selenium import webdriver
Imports the Selenium WebDriver module required to automate the browser.
Launch Chrome Browser
driver = webdriver.Chrome()
Creates a new Chrome browser session.
Open the Login Page
driver.get("https://the-internet.herokuapp.com/login")
Navigates to the Selenium practice login page.
Retrieve the Current URL
driver.current_url
Returns the complete URL of the page currently displayed in the browser.
For this example, Selenium returns:
https://the-internet.herokuapp.com/login
Validate the Current URL
assert driver.current_url.endswith("/login")
Checks whether the current URL ends with /login.
Using endswith() makes the validation more flexible because it ignores the domain name and focuses only on the required URL path.
Close the Browser Session
driver.quit()
Closes the browser and ends the WebDriver session.
Browser Execution Flow
Launch Browser
│
▼
Open Login Page
│
▼
Retrieve Current URL
│
▼
Compare URL with Expected Value
│
▼
Assertion Passes
│
▼
Close Browser
Automation Testing Example
Suppose you’re testing an e-commerce application.
After clicking the Login button, the application should redirect the user to the dashboard.
Instead of checking multiple page elements immediately, you can first verify the URL.
assert driver.current_url.endswith("/dashboard")
If the URL is correct, the automation script continues with the remaining validations.
Real-World Example
Consider an online banking application.
After a customer logs in successfully, the application redirects them to the account summary page.
An automation script can verify the navigation using the current URL.
assert driver.current_url.endswith("/account-summary")
This confirms that the customer has successfully reached the intended page.
Common Mistakes Beginners Make
Comparing the Entire URL Unnecessarily
Incorrect
assert driver.current_url == "https://the-internet.herokuapp.com/login"
If the application’s domain changes between environments (Development, QA, Production), this assertion may fail even though the correct page has loaded.
Better Approach
assert driver.current_url.endswith("/login")
This verifies the important part of the URL while making the test more maintainable.
Using current_url() Instead of current_url
Incorrect
driver.current_url()
Error
TypeError
The current_url is a property, not a method.
Correct
driver.current_url
Checking the URL Before Navigation Completes
Incorrect
driver.get(url)
print(driver.current_url)
On slow websites, the page may still be loading.
Use explicit waits whenever necessary before validating the URL.
Best Practices
Use
current_urlto validate successful navigation.Prefer
endswith()orinwhen validating URL paths.Wait for the page to load completely before retrieving the URL.
Combine URL validation with page title or element validation for more reliable tests.
Avoid hardcoding complete URLs unless absolutely necessary.
Conclusion
The current_url property is an important Selenium WebDriver property used to retrieve and validate the URL of the currently loaded web page.
It is widely used in automation testing to verify page navigation, redirects, login flows, and routing behavior. Validating URLs helps ensure that users are directed to the correct pages during automated test execution.
Frequently Asked Questions (FAQs)
What does the current_url property return?
It returns the complete URL of the currently loaded web page.
Is current_url a method or a property?
It is a property.
Does current_url require parentheses?
No.
Use:
driver.current_url
Why is endswith() commonly used with current_url?
It allows validation of the important URL path without depending on the complete domain, making automation scripts more flexible.
Can current_url be used for page validation?
Yes.
It is one of the most commonly used properties for validating navigation and page redirection in Selenium automation.
Key Takeaways
The
current_urlproperty returns the complete URL of the current web page.The syntax is
driver.current_url.It is a property, not a method.
It is widely used to validate navigation and page redirection.
Using
endswith()makes URL validation more flexible.Combine URL validation with page title or element validation for robust automation tests.
Understanding
current_urlis an important Selenium interview topic.
