I’m trying to write Playwright tests and I see examples using getByTestId(), but I can’t seem to get it working in my Python scripts. What am I missing?
@saanvi.savlani make sure you’re using the locator.get_by_test_id(“your-id”) method from the Locator API (not page.get_by_test_id unless you’re on the latest version). This worked for me:
page.locator("[data-testid='submit-btn']")
I agree with @saanvi.savlani but just to add, I think with newer versions of Playwright, you can just use:
page.get_by_test_id("submit-btn")
But ensure your data-testid attribute is spelled correctly and actually exists in the DOM when the script runs.
I once missed it due to a timing issue, adding a wait helped.
I personally prefaer adding a custom selector engine for test IDs using set_test_id_attribute("data-id")
if you’re not using the default attribute.
That way your team can use any custom attribute and still stick to test IDs cleanly.
Hope this helps , @mark-mazay thanks for the update will try using it.