Just for educational purposes, is there a way to do it?
I ran:
playwright codegen demo.playwright.dev/todomvc
and got these:
page.locator("video").click()
page.get_by_label("YouTube Video Player").click()
page.get_by_label("Play keyboard shortcut k").click()
But it doesn't work.
One option is to use the keyboard shortcut k:
This may seem roundabout, but YouTube can change DOM elements any time, but probably won't change their user-facing keyboard shortcuts used by millions. It's a good thing to avoid CSS selectors in most cases. Even labels and clicks can be unstable.
For future reliability, the main factor is making sure the keyboard key isn't triggered before the page loads and relevant listeners are ready. At the time of posting, however, the above code seems consistent without adding a delay.