0

I'm using playwright to enter my email, navigate to a folder that automatically receives some emails every day. The body of the emails may contain a link to download a report or not, depending on whether there was production on that day.

The program enters email by email, clicks on the download link, and after downloading it goes to another email. However, when the first email without the download link is opened, the program stops working. The following error appears:

playwright._impl._api_types.TimeoutError: Timeout 30000.0ms exceeded while waiting for event "download"
=========================== logs ===========================
waiting for event "download"
============================================================

The code I'm using is the one below.

    if page.locator("a", has_text="Download"): # Line that is supposed to be testing for the download link.
        with page.expect_download() as download_info:
            page.locator("a", has_text="Download").click()
        download = download_info.value
        path = download.path()
        download.save_as(path=download.suggested_filename)

        # Renaming and moving file
        time.sleep(1)
        check_file_downloaded()
        rename_and_move_file()

    elif page.locator('text=There are no data'): # Line that is supposed to be checking when there is no download link.
        print('{} no production report.'.format(filename), end='')
        continue

This code snippet is inside a for structure that should analyze all the emails that were received in the folder.

Any suggestions on how can I correctly test whether or not there will be the download link and if there is not the program, proceed to the analysis of the next email normally?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Wrap the problematic code in a try / except block. If no link is present just print it to a log. Also Time.sleep(1) is a really bad idea. Sleep for at least 5 seconds, there is no point in waiting only 1 second. – Tal Angel Sep 19 '22 at 14:45
  • "Sleep for at least 5 seconds, there is no point in waiting only 1 second." -- actually, you shouldn't be sleeping at all. 5 second sleep is unnecessary and a serious slowdown. Use the prescribed event-driven API, waiting for selectors, functions, network requests/responses, etc. As for the question, a runnable [mcve] would be nice. – ggorlen Dec 03 '22 at 16:08

0 Answers0