Introduction
The title property is one of the most commonly used browser properties in Selenium WebDriver. It is used to retrieve the title of the currently loaded web page from the browser tab.
Every web page contains a title that appears on the browser tab. Selenium allows us to retrieve this title using the driver.title property and verify whether the correct page has been loaded.
The title property is widely used in automation testing to validate page navigation, verify successful login, confirm redirects, and ensure that users land on the expected page.
In this tutorial, you will learn what the title property is, why it is used, its syntax, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.
What is title?
The title property returns the title of the currently loaded web page.
The title is displayed on the browser tab and is defined using the HTML <title> tag.
Unlike browser commands such as close() or quit(), the title property does not perform any action. Instead, it retrieves information from the currently loaded page.
Why Do We Use title?
The title property is commonly used to:
Verify that the correct page has loaded.
Validate successful navigation.
Confirm page redirection.
Verify login success.
Perform assertions during automated testing.
Improve test reliability by validating page identity.
Syntax
driver.title
The title property does not accept any parameters.
Practical Example
The following example demonstrates how to retrieve and validate the title of a web page.
The automation script opens the Selenium practice website and verifies that the page title is “The Internet”.
from selenium import webdriver
# Topic: 5. Browser Commands - title
# Practice site: https://the-internet.herokuapp.com/
#
# driver.title returns the current page title from the browser tab.
def test_get_page_title():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/")
assert driver.title == "The Internet"
finally:
driver.quit()
Output
Browser launched successfully.
Website opened successfully.
Page title retrieved:
"The Internet"
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 Practice Website
driver.get("https://the-internet.herokuapp.com/")
Navigates to the Selenium practice website.
Retrieve the Page Title
driver.title
Returns the title of the currently loaded web page.
For this website, the returned title is:
The Internet
Validate the Page Title
assert driver.title == "The Internet"
Checks whether the retrieved page title matches the expected title.
If both values are equal, the assertion passes successfully.
Close the Browser Session
driver.quit()
Closes the browser and ends the WebDriver session.
Browser Execution Flow
Launch Browser
│
▼
Open Website
│
▼
Retrieve Page Title
│
▼
Compare with Expected Title
│
▼
Assertion Passes
│
▼
Close Browser
Automation Testing Example
Suppose you’re testing an online shopping application.
After logging in successfully, the user should be redirected to the dashboard page.
Instead of checking every element immediately, you can first verify the page title.
Example:
assert driver.title == "Dashboard"
If the title matches, the test continues.
Otherwise, the test fails immediately.
Real-World Example
Consider an online banking application.
After entering valid login credentials, the application should navigate to the Account Summary page.
An automation script can verify the page by checking its title.
assert driver.title == "Account Summary"
This confirms that the user has successfully reached the correct page.
Common Mistakes Beginners Make
Comparing with the Wrong Title
Incorrect
assert driver.title == "Internet"
Error
AssertionError
The actual title is “The Internet”, not “Internet”.
Correct
assert driver.title == "The Internet"
Checking the Title Before the Page Loads
Incorrect
driver.get(url)
print(driver.title)
On slow websites, the title may not be fully loaded.
Use explicit waits whenever necessary before validating the title.
Using title() Instead of title
Incorrect
driver.title()
Error
TypeError
The title is a property, not a method.
Correct
driver.title
Best Practices
Always validate the page title after navigation.
Use meaningful assertions while comparing titles.
Ensure the page has completely loaded before retrieving the title.
Use the
titleproperty to verify successful page navigation.Combine title validation with URL and element validation for more reliable tests.
Conclusion
The title property is one of the simplest yet most useful browser properties in Selenium WebDriver. It allows automation scripts to retrieve and validate the title of the currently loaded web page.
Validating page titles helps ensure that users are navigated to the correct pages, making automation scripts more reliable and easier to maintain.
Frequently Asked Questions (FAQs)
What does the title property return?
It returns the title of the currently loaded web page.
Is title a method or a property?
It is a property.
Does title require parentheses?
No.
Use:
driver.title
Can the title property be used for validation?
Yes.
It is commonly used to verify that the expected page has loaded successfully.
What happens if the expected title does not match?
The assertion fails and Selenium raises an AssertionError.
Key Takeaways
The
titleproperty returns the title of the current web page.The syntax is
driver.title.It is a property, not a method.
It is commonly used to validate successful page navigation.
The
titleproperty is widely used with assertions in Selenium automation tests.Always verify the page has loaded before checking the title.
Page title validation is one of the most common Selenium interview questions.
