How to catch browser dialogs (e.g. "download or open") with Playwright for Python?
Question:
I was trying to handle a browser dialog with Playwright for Python in Firefox (in this case, but I guess the browser does not matter), i.e. a dialog that was opened by Firefox to ask whether to open or save a file. The dialog pops up after I click a button. The link to the file is not exposed, so I cannot download it in another way.
I tried catching a dialog
event:
with page.expect_event("dialog") as page_info:
button = page.querySelector('button[id="download"]')
button.click()
which times out. I then thought I found the solution in this GitHub ticket. However, the following did not work either:
page.on("dialog", lambda dialog: dialog.accept())
page.click("button")
Do these kinds of dialog – in contrast to dialogs e.g. raised by JavaScript alert()
– not trigger a dialog
event? I think they may not, judging by the answers to this post: Is it possible to catch browser's File Open/Save dialog event using Javascript.
If so, how can I accept or dismiss such a dialog using python-playwright?
Answers:
Try this:
page.once("dialog", lambda dialog: dialog.accept())
So, it seems that this is simply not directly possible with python-playwright
, nor is it going to be. From this discussion, I gathered that the dialog…
"[…] is not a part of the web, it is a part of the browser, so we don’t automate it."
There are workarounds, e.g. to trigger a download in a different way, but this would then not involve handling the browser dialog, as stated in the question.
I was trying to handle a browser dialog with Playwright for Python in Firefox (in this case, but I guess the browser does not matter), i.e. a dialog that was opened by Firefox to ask whether to open or save a file. The dialog pops up after I click a button. The link to the file is not exposed, so I cannot download it in another way.
I tried catching a dialog
event:
with page.expect_event("dialog") as page_info:
button = page.querySelector('button[id="download"]')
button.click()
which times out. I then thought I found the solution in this GitHub ticket. However, the following did not work either:
page.on("dialog", lambda dialog: dialog.accept())
page.click("button")
Do these kinds of dialog – in contrast to dialogs e.g. raised by JavaScript alert()
– not trigger a dialog
event? I think they may not, judging by the answers to this post: Is it possible to catch browser's File Open/Save dialog event using Javascript.
If so, how can I accept or dismiss such a dialog using python-playwright?
Try this:
page.once("dialog", lambda dialog: dialog.accept())
So, it seems that this is simply not directly possible with python-playwright
, nor is it going to be. From this discussion, I gathered that the dialog…
"[…] is not a part of the web, it is a part of the browser, so we don’t automate it."
There are workarounds, e.g. to trigger a download in a different way, but this would then not involve handling the browser dialog, as stated in the question.