简体   繁体   中英

Closing a confirm() popup with playwright in python

I am trying out this bot challenge in Python with Playwright. After I click the submit button a Javascript dialog pops up which I have to confirm. The Playwright docs claim this code below should do the trick.

page.on("dialog", lambda dialog: dialog.accept())

But I can't get it to work.

The description of the page.on() says: "Emitted when the page closes.--- Emitted when JavaScript within the page calls one of console API methods, eg console.log or console.dir. Also emitted if the page throws an error or a warning."

You have to write the page.on("dialog", lambda dialog: dialog.accept()) code before you press on button which is generating the popup. Playwright works with the listner so you should first start listner then click on element which is generating the popup.

page.on("dialog", lambda dialog: dialog.accept())
page.locator(#xyz).click() // Element opening alert window

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM