Rayrun
← Back to Discord Forum

Can I "resolve" a locator?

Hi,

I have an element (rule) with with an undefined name and I would like to rename, that.

e.g.

const newRule = page.locator('div.card', { hasText: /Unnamed Rule/ });

// Setup name.
await newRule.getByRole('heading').first().dblclick();
await newRule.getByRole('textbox').fill(ruleName);

But once I dblclick the heading, the heading is removed and replaced with a form, so the selector is not valid anymore. But the next selector then evaluates the full selector from the start. Can I just "pin" the locator or resolve it, so that it points to the rule card?

This thread is trying to answer question "Can I 'pin' or 'resolve' a Playwright locator to a specific element after the DOM changes?"

3 replies

Not really because the locator will be used by fill() with auto-waiting and all. The Locator is more like a 'pointer' than the actual element.

I'm not sure to understand if it makes your test failing or you just want to optimize things...

Anyway in case the fill relies on the doubleclick, you can rely on Promises:

await Promise.all([
  newRule.getByRole('heading').first().dblclick(),
  newRule.getByRole('textbox').fill(ruleName),
]);

Related Discord Threads

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