i am doing below to click
await this.page.getByRole(‘tab’, { name: "’"+statusHolder+"’"}).click();
i am getting error message
waiting for getByRole(‘tab’, { name: ”Plan trip/buy tickets” })
if i hardcode this it works fine.
await this.page.getByRole(‘tab’, { name: ‘Plan trip/buy tickets’}).click();
in the error it is appending extra at the end.
await this.page.getByRole(‘tab’, { name: "’"+statusHolder+"’"}).click();
await this.page.getByRole(‘tab’, { name: ‘Plan trip/buy tickets’}).click();
how can i paramterized the get by role in javascript playwright.
2
Answers
just now i tried with this statement and it worked await this.page.getByRole('tab', { name : statusHolder}).click();
The issue you’re encountering is due to the way JavaScript handles string concatenation and escape characters. When you concatenate strings, especially those containing special characters like
/
, you need to ensure that the resulting string is correctly formatted.Here’s a more robust way to handle this using template literals, which are enclosed by backticks (
`
) and allow for embedded expressions:This approach ensures that the string is correctly interpreted without the need for extra escape characters.
Here’s a complete example:
If
statusHolder
is a variable that contains the string'Plan trip/buy tickets'
, this will work correctly without the extra backslashes.Alternatively, if you prefer to stick with string concatenation, you can escape the backslashes manually:
This ensures that any single quotes within
statusHolder
are properly escaped.