Reputation: 283
I'm using Playwright to upload a file and download the result. When the inputfiles are large, and it takes a long time to process I get a timeout from playwright; it takes to long for the button "Download" to appear.
raise exception
playwright._impl._api_types.TimeoutError: Timeout 30000.0ms exceeded while waiting for event "download"
=========================== logs ===========================
waiting for event "download"
============================================================
How can I let playwright wait longer on that specific event?
with page.expect_download() as download_info:
page.locator("text=Download").click()
#todo: wait longer?
download = download_info.value
# expect(page).to_have_url("http://localhost:8080/swagger/#/NER/post_ner")
path = download.path()
suggested_filename = file_out
download.save_as(suggested_filename)
Upvotes: 9
Views: 14467
Reputation: 1225
Doc says you are going the right way
# Wait for the download process to complete and save the downloaded file somewhere
download.save_as("/path/to/save/at/" + download.suggested_filename)
But, looks like your button misclicked (is locator valid?) or some network event missed. So, play around expect(page.locator("text=Download")).to_be_enabled(timeout=100_000)
first, then try to wrap something like that using network devTool:
with (
page.expect_response(**/swagger/#/NER/post_ner) as response_info,
page.expect_download() as download_info
):
# 1. make sure you are on the right page
page.goto(/navigate/to/download/view)
response = response_info.value
assert response.ok
# 2. wait for button to enabled
expect(page.locator("text=Download")).to_be_enabled()
page.locator("text=Download").click()
download = download_info.value
path = download.path()
suggested_filename = file_out
download.save_as(suggested_filename)
Upvotes: 0
Reputation: 532
Not sure if this will help, but I have used the code below successfully many times:
with page.expect_download(timeout=0) as download_info:
Setting timeout=0 causes it to wait for as long as it takes to download.
Upvotes: 2
Reputation: 9
You can also set the timeout to 0 if your not concerned with it taking too long. Though this can lead to it potentially taking hours...or never end if it gets stuck loading. A timeout of some value other than 0 is probably best practice.
page.locator("text=Download").click(timeout=0)
Upvotes: -1
Reputation: 21607
If you know that the click
will take a while, you can set a timeout:
page.locator("text=Download").click(timeout=60000)
Upvotes: 8