If you want to make the most of your Selenium test automation, you need to understand how to retrieve web page titles. Page titles will help you double-check navigation accuracy and if your pages load properly.
Checking that your application behaves the way you expect is an important part of any automated test. It gives you more confidence that your test cases are actually checking the right things. Let’s go over it in detail with this article.
What is the getTitle() Command?
The getTitle() method in Selenium returns the title of the page currently open in the browser. It reads the value inside the HTML <title> tag and returns it as a string, which you can use in your test.
One of its most common uses is checking whether the browser has landed on the right page after an action like clicking a link, submitting a form, or completing a login. If the returned title matches what you expect, it’s a quick indication that navigation worked as intended.
Although it’s a simple method, getTitle() is widely used because it’s fast and easy to implement. It’s especially useful in multi-page test flows, where confirming the page title helps make your automated tests more reliable.
What is it mostly used for:
- Cross-Browser Testing: Testing across different browsers to make sure page titles are consistent across different browsers.
- Functional Testing: Inspect basic functionality and test that the page title is aligned with the expected functionality.
Fundamental Syntax
The syntax for using the getTitle() method in Selenium WebDriver is simple:
String title = driver.getTitle();
In this syntax:
- driver: Refers to an instance of the WebDriver object, which is used to interact with the web browser.
- getTitle(): The method that retrieves the title of the current page.
- title: A variable of type String that stores the returned page title.
The getTitle() method retrieves the title of the currently loaded web page, as defined in the HTML <title> tag. It returns the title as a string, which can then be used in assertions or further logic within your test.
For example, you could compare the retrieved title with an expected title to validate correct navigation or page content during your test.
Since getTitle() only reads the page title, it doesn’t interact with or modify the application in any way. That makes it a simple and reliable way to check navigation and page state during automated testing.
Read More: Selenium WebElement Commands
How to Use getTitle() Effectively?
Let’s see how getTitle() works in a simple Selenium test. The example below uses Chrome, but the same approach works with any browser supported by Selenium.
Step 1: Set up your Selenium project
Before writing your test, make sure your project is ready to run Selenium.
- Add the Selenium WebDriver dependency using Maven or your preferred build tool.
- Download the appropriate browser driver, such as ChromeDriver if you’re testing on Google Chrome.
- Configure the driver so Selenium can launch the browser.
Step 2: Open the web page
Use the get() method to launch your browser and navigate to the page you want to test.
Step 3: Retrieve and check the page title
Once the page has loaded, call getTitle() to fetch the page title. Store the returned value in a string and compare it with the title you expect. This gives you a quick way to check that the browser has landed on the correct page.
Example (Java):
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GetPageTitleExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
String pageTitle = driver.getTitle();
System.out.println("Page Title: " + pageTitle);
if (pageTitle.equals("Expected Title")) {
System.out.println("Title matches the expected value.");
} else {
System.out.println("Title does not match.");
}
driver.quit();
}
}What’s happening here?
- driver.get() opens https://www.example.com in the browser.
- driver.getTitle() reads the page title and stores it in the pageTitle variable.
- The returned title is compared with the expected value to check that the correct page has loaded.
- Finally, driver.quit() closes the browser and ends the session.
Common Use Cases for getTitle()
Understanding how to use getTitle() effectively in Selenium can significantly enhance the reliability of your test automation. Below are some common use cases where getTitle() proves invaluable in web testing:
1. Navigation Validation
One of the most common use cases for the getTitle() method is to check if a web page redirects properly.
After you have executed actions that cause a page navigation, such as clicking a link or submitting a form, it is essential to check whether the browser has successfully navigated to the proper page.
By using the getTitle() method, you can retrieve the title of the newly loaded page and verify that it matches the expected title.
For example, after logging in, the script verifies that the user lands on the dashboard:
python
driver.get("https://example.com/login")
# Perform login actions...
driver.find_element("id", "username").send_keys("testuser")
driver.find_element("id", "password").send_keys("password")
driver.find_element("id", "login-button").click()
# Verify navigation success
expected_title = "Dashboard - Example"
assert driver.title == expected_title, "Navigation failed!"If the page title does not match, the script detects a navigation failure, helping identify potential issues like incorrect redirects, authentication failures, or broken links.
2. Page Verification
In automation testing, verifying that the correct web page has loaded is crucial. The getTitle() method helps confirm that the browser is displaying the expected page before proceeding with further test steps. This prevents false positives caused by navigation errors or incorrect redirections.
expected_title = "Login - MyApp" actual_title = driver.title if actual_title == expected_title: print("Page loaded correctly.") else: print("Page verification failed!")For example, if a test script navigates to a login page, it can verify the title to ensure the page has loaded correctly. If the title does not match, the script can halt further execution or trigger an alert.
3. Automated Assertions
Assertions help ensure that the title of a web page matches the expected value, preventing test scripts from proceeding if a mismatch occurs. Automated assertions using getTitle() improve test reliability by automatically failing incorrect navigation tests.
from selenium import webdriver driver = webdriver.Chrome() driver.get("https://example.com") assert driver.title == "Example Domain", "Page title does not match!" driver.quit()If the title is incorrect, the assertion fails, flagging the test as unsuccessful. This approach is particularly useful in login flows, redirects, and multi-page navigation testing.
4. Cross-Browser Testing
Cross browser testing ensures that a web application functions uniformly across different browsers, including Chrome, Firefox, and Edge.
In this regard, getTitle() can be applied to ascertain that the page title remains uniform across the different browsers. This is important because differences in page rendering or navigation behavior could occur when different browsers interpret web pages differently.
By retrieving the title by using getTitle() in each browser and comparing them, testers can ensure that even if the underlying browser differs, the user experience is similar.
5. Functional Testing
Functional testing is done to ensure that the web application does the right thing- the right thing in this context is that it performs all the intended functions correctly. During automated testing, using getTitle() can help confirm that the proper pages load as part of specific workflows.
For example, following a search or filtering through products on an e-commerce site, you might wish to confirm that the title reflects the correct page: “Search Results” or “Filtered Products“.
This is very useful for multi-step workflows, ensuring that every page loads in the correct order. For this reason, checking at each step of the workflow against the title will assure you that the application is working correctly and taking the user to the right pages.
Read More: findElement and findElements in Selenium
Error Handling and Troubleshooting
getTitle() is one of the simplest Selenium methods, but that doesn’t mean it always returns what you expect. Most problems happen because the browser isn’t on the page you think it is yet, or Selenium is still pointing to a different tab or window.
If you run into unexpected results, the issue is usually easy to trace once you know where to look.
Common Issues:
- Calling getTitle() before creating the WebDriver
One of the most common mistakes is trying to use getTitle() before the browser has even been launched. In this case, Selenium throws a NullPointerException because there’s no active WebDriver session to work with.
Always create your WebDriver instance before navigating to a page or calling any WebDriver methods.
- Reading the title from the wrong tab or window
If your test opens a new tab or browser window, Selenium doesn’t automatically switch to it. Calling getTitle() too early may return the title from the previous page instead of the one you intended to test.
Switch to the correct browser context before reading the page title.
Read More: Exception Handling in Selenium WebDriver
Our Tips:
- Wait until the page has finished loading: Modern applications often update content after the initial page load. Using an explicit wait before calling getTitle() helps avoid reading a title that’s still changing.
- Keep track of browser tabs and windows: Whenever your test opens a new tab, switch to it immediately instead of assuming Selenium has already done it.
- Don’t rely on the page title alone: A matching title is a good sign, but it shouldn’t be your only assertion. Pair it with URL checks or important page elements for stronger test coverage.
- Print the title while debugging: If a test fails unexpectedly, logging the value returned by getTitle() often makes it obvious whether the browser landed on the wrong page or navigation was never completed.
Conclusion
The getTitle() method may be small, but it’s one of the easiest ways to confirm that your Selenium test is on the right page. Whether you’re testing a login flow, validating a redirect, or moving through a multi-page workflow, comparing the page title with the expected value adds a quick layer of confidence to your tests.
Like any assertion, though, it works best alongside other checks. Combining getTitle() with URL validation, element assertions, and proper waits gives you more reliable tests that are less likely to fail because of timing issues or page changes. Used thoughtfully, getTitle() becomes a simple but valuable part of your automated testing workflow.



