What is the method to locate and extract text from a specific HTML element using Playwright?
playwright - Unable to click on any element using GetByText - Stack Overflow
typescript - How to check for some text on a webpage using playwright? - Stack Overflow
Playwright selecting element with text= or hastext with exact match - Stack Overflow
Videos
In the first case you need a locator, in the second case your locator found too many elements.
You do need to be interested in the exact elements (even if it can be any element on page), how will you else locate them and assert if the text is visible ?
You should find an element on the page by using the text as locator, and check if its present, thereby knowing the text is on page e.g.
UPDATE
comments are correct and i found that there's more correct ways to implement this, you should do as following instead - above reasoning still persists.
const element = await expect(page.getByText('QUEUED')).toBeVisible();
so you should care about finding a element regardless of what element it is, as long as you'r trying to verify its presence on the page.
You can check the text anywhere on the page like this:
await expect(
page.getByRole('main').filter({hasText: "Lorem ipsum, bla, bla, bla"}),
).toBeTruthy();
text body can be escaped with single or double quotes to search for a text node with exact content
pageNumberButton(page, table_id, page_number) {
page.locator(`[aria-controls=${table_id}]`)
.filter({ has: page.locator(`text="${page_number}"`) });
}
I found an option. Create a regex object and pass that in for hasText.
pageNumberButton(page, table_id, page_number) {
const regexNumber = new RegExp(`^${page_number}$`);
page.locator(`[aria-controls=${table_id}]`, {hasText: regexNumber});
}`