Skip to content

[Feature]: Add BrowserContext expect_download Methods support #2978

@baijiu-in-my-cup

Description

@baijiu-in-my-cup

🚀 Feature Request

Performs action and waits for a new Download on BrowserContext.

Example

No response

Motivation

The page.expect_download() method seens cannot handle scenarios where the download is triggered from a newly opened popup window.

To work around this, I'm currently using page.context.expect_event('page') to capture the new page and then active trigger the download in this page. However, this bring about a new question,I will trigger downloads repeatedly.

    def _download_file(self) -> str:
        def download_page_handler(page: Page):
            with page.expect_download() as download_info:
                page.reload()
            download = download_info.value
            self.logger.debug(f"Suggested Filename:{download.suggested_filename}")
            suffix = pathlib.Path(download.suggested_filename).suffix
            download_path: pathlib.Path = (
                generate_filepath(
                    out_dirs=[self.params.base_output_path,
                              self.params.account_number],
                    suffix=suffix,
                )
                if not self.params.output_path.suffix
                else self.params.output_path.with_suffix(suffix)
            )
            download.save_as(download_path)
            self.logger.info(f"[{self.params.account_number}] Save To {download_path}")
            page.close()
            return str(download_path)

        with self.page.context.expect_event('page') as event_info:
            self.win_frame.locator('.tableFooter').get_by_text(
                'Download all', exact=True).click(no_wait_after=True)
            self.win_frame.get_by_role("listitem").filter(
            has_text="Excel").click()

        return download_page_handler(event_info.value)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions