'How to handle comfirm alert in Playwright python

I was struggling to how to handle alert box in playwright, I was following this documentation https://playwright.dev/python/docs/dialogs#alert-confirm-prompt-dialogs

Here is my snippet of code

page.click('text=STOP BUTTON'); #Prompt appears are you sure you want to stop? xOK xCancel
page.on("dialog", lambda dialog: dialog.accept())
page.on("dialog", lambda dialog: print(dialog.message))

page.click("OK")
#page.click('text=OK') 


Solution 1:[1]

What dialog.accept() does is to accept (click OK) the dialog. page.click("OK") only works with DOM (HTML) elements, not with browser dialogs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hardkoded