Locating elements efficiently is crucial in any automated testing framework, and Playwright offers powerful built-in locators to simplify this task. One such useful method is the getByTitle locator, which allows you to select elements based on their title attribute. This can be especially helpful when other attributes like id, text, or label are unavailable. In this guide, we’ll explore how to use the getByTitle locator in Playwright with clear examples and best practices.
What is getByTitle in Playwright?
The getByTitle locator in Playwright allows you to select elements based on the value of their title attribute. This is especially useful for elements like icons, buttons, or images that use title as a tooltip or for accessibility purposes.
Syntax:
await page.getByTitle('Your Title Text');
This line will locate an element with the exact title “Your Title Text”.
When to Use getByTitle Locator
Use getByTitle when:
- The element has a unique title attribute.
- If you’re working with icons or images that don’t have accessible inner text.
- You need a fallback when getByRole or getByLabel doesn’t work.
How to Use getByTitle Locator
Suppose you have the HTML below for the button.
<button title="Download PDF">Download</button>
<a href="/help" title="Help Section">Help</a>
<input type="text" title="Enter your name" />
You can see that button has a title attribute.
To locate that button using getByTitle in Playwright, you can use the following test script.
Playwright Example to Locate by getByTitle:
// Using Playwright with JavaScript or TypeScript
import { test, expect } from '@playwright/test';
test('get element by title attribute', async ({ page }) => {
await page.goto('https://your-website.com');
// Locate the button using getByTitle
const downloadButton = page.getByTitle('Download PDF');
await expect(downloadButton).toBeVisible();
// Locate the input field
const nameInput = page.getByTitle('Enter your name');
await nameInput.fill('John Doe');
});

Real-World Use Case
Imagine you’re testing a web app that uses only icons in a toolbar, and each icon has a tooltip via the title attribute. Using getByTitle, you can click on the right icon based on its purpose.
await page.getByTitle('Settings').click();
await page.getByTitle('Upload').click();
This method keeps your tests clean, readable, and easy to maintain.
getByTitle vs Other Playwright Locators
Locator | Best For | Example |
getByRole | Buttons, links, UI components | getByRole(‘button’) |
getByLabel | Form fields with associated labels | getByLabel(‘Email’) |
getByPlaceholder | Inputs with placeholder text | getByPlaceholder(‘Search’) |
getByTitle | Icons, images, tooltips with title attribute | getByTitle(‘Download’) |
Use getByTitle when others don’t apply or when you’re testing accessibility features.
Benefits of Using getByTitle
- Targets hidden or tooltip elements
- More semantic than CSS selectors
- Useful when no unique ID or label is present
Common Errors and Troubleshooting
- Error: locator not found
- Fix: Ensure the title attribute value is accurate and spelled correctly.
- Issue: Case sensitivity
- Fix: Remember title is case-sensitive, match it exactly.
- Check for typos or extra spaces in the title.
- Use browser dev tools (Inspect Element) to verify the exact title text.
- You can use page.locator(‘[title=”Your Title”]’) if getByTitle doesn’t work.
Best Practices for Using getByTitle
- Be exact: Match the title text precisely. It is case-sensitive.
- Avoid dynamic titles: Don’t rely on titles that change frequently or are user-generated.
- Combine with expect: Always verify element visibility or state before interacting.
Related Articles
- Selector by Text in Playwright
- Playwright getByRole Locator
- getByAltText locator in Playwright
- getByPlaceholder locator in Playwright
Final Thoughts
The getByTitle Playwright locator is a simple yet powerful way to interact with elements that rely on the title attribute. It helps improve test clarity and maintainability, especially when dealing with UI components that lack text labels. Use it wisely in your automation scripts to write robust and accessible tests.
FAQs About getByTitle in Playwright
What does getByTitle do in Playwright?
It selects an element by its title
attribute, often used for tooltips or icons.
Is getByTitle case-sensitive?
Yes, getByTitle matches the exact text including case and spacing.
What’s the alternative to getByTitle?
You can use page.locator('[title="..."]')
or other locators like getByRole
or getByLabel
.
Can getByTitle be used with images?
Yes, if the image has a title attribute, it can be selected using getByTitle.

Hi, I’m Aravind — a seasoned Automation Test Engineer with over 17 years of hands-on experience in the software testing industry. I specialize in tech troubleshooting and tools like Selenium, Playwright, Appium, JMeter, and Excel automation. Through this blog, I share practical tutorials, expert tips, and real-world insights to help testers and developers improve their automation skills.In addition to software testing, I also explore tech trends and user-focused topics, including Snapchat guides, codeless test automation, and more.