I need to click on an object using Playwright for Python! Sometimes the element's CSS changes, or it doesn't have an ID. I usually use
page.click(XPATH)
, But I know there are many other ways like:
1 - page.get_by_text("Get Started").click()
2 - page.get_by_role("button", name="Submit").click()
3 - page.get_by_selector("#login-button").click()
4 - page.get_by_selector("//div[@class='user-agent']").click()
5 - locator = page.locator(".user-agent")
locator.click()
Which has the highest probability of success?
Playwright has written som good advices: https://playwright.dev/docs/best-practices#best-practices (they are in the node.js docs, but the message is the same with Python). Personally, I agree with them: Write test based on whats visible to the user (get_by_text, get_by_role), tests depending on css/xpath etc. is more prone to flakiness.