Rayrun
← Back to Discord Forum

Error accessing the element with getByTestId

Sample HTML Code: <div id="row"><span data-testid="spanD" click="clicked()"></span></div> Playwright Code to access element:

const editBtn = page.locator(id='#row') await editBtn.getByTestId('spanD').click()

This always fails and times out. When i print the getByTestId it adds an 's' to the id given

This thread is trying to answer question "How to fix the error when accessing an element with getByTestId in Playwright?"

4 replies

Any help would be highly appreciated, i am sure i am missing out something simple

debbieobrien
debbieobrien

try this @shyam

debbieobrien
debbieobrien
await page.setContent(`<div id="row"><span data-testid="spanD" click="clicked()">button</span></div>`);

  const editBtn = page.locator('#row')
  await editBtn.getByTestId('spanD').click()
});```

Thank You @debbieobrien , but my issue was in the way in which i was passing data-testid in angular, i had to pass it as attr.data-testid which was correctly displaying the data-testid.

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].