← Back to Discord Forum

Pass regex (id) to locator()

_kalkleiste_posted in #help-playwright
Open in Discord

Hallo guys. I would like to know if it's possible to pass a regex which matches a fraction of an ID to locator() to get the elements

In detail: We would like to use https://www.zkoss.org/ for the frontend. The disadvantage of the framework is, it generates random id's for each element/component. Every time you open a window/page the DOM is new. Every table, row... etc. get a new ID. However a fraction of each ID is static (id=list_<random_suffix>) . Therefore I though I can use regex like locator(/#list_. /) but it does not work. The xpath/css approach locator(//[@id="list_. *"]) does not work as well. Do I get right that I cannot pass regex for classes, id's, DOM-elements?

This thread is trying to answer question "Is it possible to pass a regex which matches a fraction of an ID to locator() to get the elements in the ZKoss frontend framework?"

7 replies

you can use css selector

page.locator('[id^="list_"]') will match element that start with list_


Thy @skorp32 it works. However I convince the devs not to use that approach. PlayWright also recommends not using css/path.

The results I get I have to filter again. Yes regex works but the output can cause new issues.

xpath is not recommended, notthing wrong with css though

even methods like getByTestId actually uses css selectors internally

I mean it's better to use playwright given methods versus raw css selector but there is nothing wrong with using css selectors in general

@skorp32: Yeah true. I prefer pw's methods.

Yeah true. I prefer pw's methods.

Related Discord Threads

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 luc@ray.run.