Get Title in Selenium WebDriver with Examples
Page titles may look simple, but they play a powerful role in UI testing. Every time you land on a web page, the browser title tells you where you are. That same logic applies to automation.
With Selenium WebDriver, you can get the page title to confirm if your test is on the right screen. If you're trying to validate navigation, this ability to get title in Selenium helps confirm that the application behaves as expected.
In this article, we’ll walk you through:
- How to get title in Selenium WebDriver
- Why page title matters in testing
- Java and Python examples using getTitle and driver.title
- How to use titles for assertions and validations
- Common issues with title fetching and how to handle them
- Best practices to keep your tests clean and stable
- And why Katalon makes title verification even easier
Let’s get started.
What is get title in Selenium?
Selenium WebDriver provides a simple method to get the title of the current page. You can use it to confirm that the browser has loaded the right content before continuing with your test.
The method is straightforward. In Java, it’s called getTitle. In Python, you can use the title property. Both return the text displayed in the browser’s title bar or tab.
You can use this value for logging, for conditional logic, or to validate that a page has loaded successfully after a navigation event. When you get title in Selenium, you add an extra layer of confidence to your automation flow.
Why use page title Selenium testing?
Getting the page title in Selenium is a quick way to confirm that your test is on the right path. It gives you a clear checkpoint without requiring complex element lookups or deep DOM interaction.
Page title checks are simple but powerful. They help ensure that your automation script is behaving as expected during key navigation steps. Here are some practical reasons to get title in Selenium:
- Confirm that the correct page has loaded after a navigation command
- Validate that the login or logout flow reaches the right destination
- Ensure redirections land on the intended page during workflows
- Check that page titles are consistent for SEO and accessibility reviews
These checks are lightweight. They add almost no overhead to your test but offer strong validation where it matters. That makes the page title one of the most accessible tools in your Selenium toolbox.
How to get title in Selenium WebDriver
Let’s walk through two quick examples to show how to get title in Selenium. These scripts will help you verify the current page title using Java and Python. You can run them as part of your existing test suite or as a standalone check.
Here is how it works in Java:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GetTitleExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://katalon.com");
String title = driver.getTitle();
System.out.println("Page title is: " + title);
driver.quit();
}
}
This code launches Chrome, navigates to Katalon, gets the page title, and prints it. You can replace the URL to match your own test case.
Now let’s see how to get the page title in Python:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://katalon.com")
title = driver.title
print("Page title is:", title)
driver.quit()
Both examples follow the same pattern. Open a browser, navigate to a page, retrieve the title, and print the result. This is the fastest way to get title in Selenium and use it for validation or debugging.
Using page titles for Assertions in Selenium
Getting the title is useful. Verifying the title adds real value. You can easily turn your title check into an assertion, making sure the browser lands on the expected page at the right time.
In Selenium, you can compare the actual title to a known string or check whether it includes specific text. This works well with test frameworks like TestNG and PyTest where assertions are part of the test flow.
Here’s how to assert the exact title in Java using TestNG:
import org.testng.Assert;
String actualTitle = driver.getTitle();
String expectedTitle = "Katalon | Simplify Web Testing";
Assert.assertEquals(actualTitle, expectedTitle);
Now let’s do a contains check in Python using PyTest:
def test_page_title(driver):
driver.get("https://katalon.com")
assert "Katalon" in driver.title
Assertions like these make your tests more meaningful. They help confirm that navigation works and that users land where they should. When you get title in Selenium, pair it with a smart assertion and use it as a real validation step.
Common issues when getting page title
Most of the time, getting the page title in Selenium works instantly. But in a few situations, you may need to add a little more handling to keep your test smooth and reliable. Here are a few scenarios where extra care helps.
- Dynamic or empty titles: Some modern web apps update the title using JavaScript after the page loads. To handle this, use an explicit wait that waits until the title becomes non-empty or matches a value.
- Timing after navigation: If your test moves quickly, the title may not reflect the new page immediately. Insert a short wait until the expected title appears before checking it.
- Multiple windows or tabs: The getTitle method returns the title of the current active window. If your flow involves switching to a new tab or popup, make sure you switch the WebDriver context to the correct window before calling getTitle.
These patterns are easy to manage once you identify them. A simple wait or a quick switch keeps your title checks reliable even in complex flows. When you get title in Selenium, use it alongside best practices for handling modern web behaviors.
Best practices for using page titles in tests
When you get title in Selenium, it gives you a fast checkpoint in your test flow. Used well, it can improve both clarity and stability. Follow these habits to make the most of title checks in your automation strategy.
- Use page titles as a supplement: Let them confirm the flow, but pair them with URL or element checks for deeper validation.
- Combine with other locators: A title match shows the page loaded, and a visible element confirms that the UI is ready for interaction.
- Use “contains” for flexible checks: When working with dynamic or long titles, match a key phrase instead of the full string.
- Apply explicit waits when needed: Use a wait that looks for the expected title. This keeps your test reliable across different load times.
These guidelines help you treat title checks as a fast and dependable signal. The getTitle method in Selenium works best when it supports a larger test flow, not when it carries all the logic alone.
Why choose Katalon for title verification tests?
Katalon is a complete automation solution built on top of Selenium. It gives you the power of WebDriver with a user-friendly interface and smarter tools that make test creation and maintenance easier. You can use Katalon to get page titles, run validations, and manage results across any environment.
With Katalon, you don't have to write every test from scratch. You can build title verification tests using a low-code approach, while still keeping full control when needed. That balance makes Katalon a strong fit for both beginners and advanced teams.
- Low-Code Test Creation: Create and run title checks without building complex scripts from the ground up.
- Cross-Browser Execution: Validate titles across thousands of browser and OS combinations using Katalon's TestCloud or your own infrastructure.
- Smart Maintenance: Self-healing locators help your tests stay stable even when page structure changes.
- Integrated Test Management: Track, schedule, and organize your title verification alongside larger test suites in one dashboard.
- Reporting and Analytics: View titles, screenshots, and logs directly from visual dashboards for faster debugging and review.
Katalon takes what you can do with Selenium and gives it structure, speed, and scale. It helps you get title in Selenium and go further with less effort and more clarity.
To start learning, visit the Katalon Docs for tutorials or explore courses at the Katalon Academy.
