Skip to content

[Feature] Allow downloads with default context when connectOverCDP is used #12543

Closed

Description

@lvyaoyu

playwright = sync_playwright().start()
executable_path = playwright.chromium.executable_path
logger.debug(f'executable_path:{executable_path}')
server = subprocess.Popen(f"{executable_path} --remote-debugging-port=9002")
time.sleep(2)
print(f'server:{server.pid}')
browser = playwright.chromium.connect_over_cdp(
endpoint_url=f"http://localhost:9002",
)

如何给这个context 赋予accept_downloads属性

context = browser.contexts[0]

重新new的context可以赋予accept_downloads属性

context1 = browser.new_context(accept_downloads=True)

Activity

  1. mxschmitt commented on Mar 7, 2022

    @mxschmitt
    Contributor
    browser = playwright.chromium.connect_over_cdp(
      endpoint_url=f"http://localhost:9002/",
    )
    context = browser.new_context(accept_downloads=True)
    page = context.new_context()

    does this work for you?

    Please formulate your issue in english, otherwise we can't support you.

  2. lvyaoyu commented on Mar 7, 2022

    @lvyaoyu
    Author

    When I used connect_over_CDP to generate a browser object, I used the browser's original context, but this context could not be used to pass a value to the accept_downloads parameter, so the file could not be downloaded

  3. lvyaoyu commented on Mar 7, 2022

    @lvyaoyu
    Author
  4. mxschmitt commented on Mar 7, 2022

    @mxschmitt
    Contributor

    Which version are you using? Playwright by default now accepts downloads, so it should just work fine when you use the default context.

  5. lvyaoyu commented on Mar 8, 2022

    @lvyaoyu
    Author

    Which version are you using? Playwright by default now accepts downloads, so it should just work fine when you use the default context.
    I have used the latest version 1.19, but it still does not work. Python version 3.8.5, win10, the code is as follows:

    import os
    import subprocess
    import time
    from loguru import logger
    from playwright.sync_api import sync_playwright, Page, Download
    
    DOWNLOADS_PATH = './data'
    
    
    def _intercept_download(download: Download):
        file_name = download.suggested_filename
        file_path = os.path.join(DOWNLOADS_PATH, file_name)
        logger.debug(f'file_path:{file_path}')
        download.save_as(file_path)
    
    def _binding_page(page: Page):
        page.on('download', _intercept_download)
    
    
    playwright = sync_playwright().start()
    executable_path = playwright.chromium.executable_path
    logger.debug(f'executable_path:{executable_path}')
    server = subprocess.Popen(f"{executable_path} --remote-debugging-port=9002")
    time.sleep(2)
    print(f'server:{server.pid}')
    browser = playwright.chromium.connect_over_cdp(
        endpoint_url=f"http://localhost:9002",
    )
    
    context = browser.contexts[0]
    context.on('page', _binding_page)
    page = context.pages[0]
    page.goto('http://127.0.0.1:8001/')
    # Click to trigger the download event
    page.click('//a[text()="PycharmProjects.rar"]')
    page.wait_for_timeout(10000)
    playwright.stop()
    server.kill()
  6. changed the title [-][Question]使用connect_over_cdp创建的browser的默认context如何设置accept_downloads属性[/-] [+][Feature] Allow downloads with default context when connectOverCDP is used[/+] on Mar 12, 2022
  7. rai-gaurav commented on Mar 18, 2022

    @rai-gaurav

    +1 for this. I have a similar scenario where I have to do performance test by uploading/downloading of files by attaching Playwright to an existing browser instance to replicate user behavior. Currently I am getting error asking to make accept_downloads as True for download to work.
    But there is no such param in connect_over_cdp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions