Rayrun
← Back to Discord Forum

is it possible to use regex to get all elements that have the same first half of their name?

ilovelamp69posted in #help-playwright
Open in Discord
ilovelamp69
ilovelamp69

so I'm looking to iterate through a list of items that populate a dropdown that have different names every day.

An example of the first locator from today's dropdown tab is get_by_role("link", name="You have not viewed this projectGREEN LINE")

is there a way I could use regex so it could locate all items in the list that start with "You have not viewed this project...." ?

I would then just iterate through them using .nth_child{x} until I get a couple timeouts in a row where I'll then break out of my while true loop.

This thread is trying to answer question "Is there a way I could use regex to locate all items in the list that start with 'You have not viewed this project....' ?"

0 replies

Answer

Sure, you can use regex in @playwright/test to find all list items that start with 'You have not viewed this project...'. Here's how you can do it:

First, use the getByRole method to select all list items (<li>) on the page.

await page.getByRole('listitem')

Next, apply a filter using the hasText option and provide a regex pattern.

.filter({ hasText: /^you have not viewed this project/i })

This regex pattern /^you have not viewed this project/i is case-insensitive and matches any text starting with 'you have not viewed this project'.

By using this approach, you'll find all list items that start with 'You have not viewed this project...'.

Remember, @playwright/test is a powerful tool for web testing. You can learn more about it in this comprehensive guide.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].