Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

page.on not working for download dialogs? #460

Closed
buddemat opened this issue Jan 24, 2021 · 7 comments
Closed

page.on not working for download dialogs? #460

buddemat opened this issue Jan 24, 2021 · 7 comments
Labels

Comments

@buddemat
Copy link

buddemat commented Jan 24, 2021

I was trying to handle a browser dialog in (in this case) firefox, i.e. a dialog opened by firefox to ask whether to open or save a file. The dialog pops up after I click a button.

I thought I found the solution in #396. However, the following did not work:

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

Do these kinds of dialog not trigger a "dialog" event? How can I accept or dismiss such a dialog?

@pavelfeldman
Copy link
Member

pavelfeldman commented Jan 24, 2021

Check out https://playwright.dev/python/docs/downloads for docs on how to manage downloads. Dialog event is for alert, confirm, prompt and onbeforeunload.

@buddemat
Copy link
Author

buddemat commented Jan 26, 2021

Thank you for your quick reply! Unfortunately, there doesn't seem to be a download event triggered either. I've added a full example that triggers the mentioned dialog for a pdf download:

with sync_playwright() as p:
    b = p.firefox.launch(headless=False,downloadsPath=os.getcwd())
    p = b.newPage(acceptDownloads=True)
    p.on("download", lambda download: print("Download event!"))
    p.goto('https://www.w3docs.com/uploads/media/default/0001/01/540cb75550adf33f281f29132dddd14fded85bfc.pdf')
    p.querySelector('button[id=\"download\"]').click()

PS: I am aware that I could just directly download the file in this example. However, in my actual case, it is a blob file which I cannot access directly.

@dgozman
Copy link
Contributor

dgozman commented Feb 1, 2021

There are multiple issues here:

  • Scripts goes directly to the pdf page. In this case, pdf is shown (depends on the browser) instead of being downloaded.
  • Script does not wait for the actual download to happen (it might start with a delay after clicking).

Here is an example that works for me:

  • It has a page with a link that forces a download (it has a download attribute).
  • It uses expect_download() to actually wait for the download to start, as suggested in the docs.
from playwright.sync_api import sync_playwright
import os

with sync_playwright() as p:
    b = p.firefox.launch(headless=False,downloads_path=os.getcwd())
    p = b.new_page(accept_downloads=True)
    p.goto('https://www.webfx.com/blog/images/assets/cdn.sixrevisions.com/0435-01_html5_download_attribute_demo/html5download-demo.html')
    with p.expect_download() as download_info:
      p.click('text=DOWNLOAD HTML DOCUMENT')
    download = download_info.value
    print(download.path())

Let me know if that does not help.

@buddemat
Copy link
Author

buddemat commented Feb 26, 2021

@dgozman Thank you very much for your answer. And while your example works fine, unfortunately, I don't seem to be able to adapt it to my problem. The thing is that in my case the embedded pdf viewer does not open as a result on a click on a link (<a>), but instead opens on a button click. Otherwise I could modify the link to add a download attribute. But once the embedded pdf viewer is open, I don't get a download event anymore when I click the button as in my example above.

Is that normal / expected behavior? As a human, I can click the download button in the embedded PDF viewer and save the file. Shouldn't playwright somehow enable me to do the same? The only workaround I came up with thus far is running in headful mode, clicking the button and then sending an Enter press. But that does not let me modify the filename, etc.

@pavelfeldman
Copy link
Member

Is that normal / expected behavior? As a human, I can click the download button in the embedded PDF viewer and save the file. Shouldn't playwright somehow enable me to do the same?

Embedded PDF viewer is not a part of the web, it is a part of the browser, so we don't automate it.

@buddemat
Copy link
Author

buddemat commented Mar 3, 2021

Ok, thank you for the clarification. I thought since the embedded PDF viewer is based on pdf.js this might still be automatable with playwright. But now that I think about it, the dialog is not part of pdf.js, so your point is valid. This can be closed then, I guess...

@buddemat buddemat closed this as completed Mar 3, 2021
@ghost
Copy link

ghost commented May 22, 2022

In some cases it can be helpful to avoid dialogs entirely. If you open Chrome in headless mode, you won't get dialogs.

browser = playwright_page.chromium.launch(headless=True,downloads_path=os.getcwd())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants