Just to add to @dimplesaini.230 solution i think that one sneaky reason why getByTestId()
fails but locator()
works is if the element is nested inside an iframe or a shadow DOM. locator()
gives you more control to dive into nested trees, but getByTestId()
might fail silently in those scopes unless you explicitly scope it.
Try scoping manually using frameLocator
or inside the shadow root:
const frame = page.frameLocator('#my-frame');
await frame.getByTestId('createTitle').fill('My Title');
Or if it’s a web component:
await page.locator('my-component').shadowRoot().getByTestId('createTitle').fill('My Title');
This subtle context boundary often explains why the two methods behave differently.