为什么当我迭代直接下载链接列表时,剧作家 chromium 实例会自动关闭?

问题描述 投票:0回答:1

我正在尝试使用剧作家循环下载文件。我必须第一次登录 salesforce,但之后链接会直接下载文件。

但就我而言,第一个文件完成下载,然后当浏览器导航到第二个网址时,我会弹出此消息,显示“下载正在进行中。无论如何都要退出 Chromium?”: Download is in progress. Quit Chromium anyway?

这是我的代码:

from playwright.sync_api import sync_playwright  
import time

# List of URLs  
urls = ["https://test.file.force.com/servlet/servlet.FileDownload?file=some_id_1", "https://test.file.force.com/servlet/servlet.FileDownload?file=some_id_2", "https://test.file.force.com/servlet/servlet.FileDownload?file=some_id_3"]  
  
# Launch the browser  
with sync_playwright() as playwright:  
    browser = playwright.chromium.launch(headless=False)  
    page = browser.new_page()  
    flag = False
    # Iterate over URLs  
    for url in urls:  
        page.goto(url)
        if(not flag):
            page.get_by_placeholder("User Email Address").fill("[email protected]")
            page.get_by_role("button", name="Continue").click()
            page.get_by_placeholder("Password").fill("test#12345")
            page.get_by_role("button", name="SSO Login").click()
            flag = True 
        time.sleep(20)
  
    # Close the browser  
    browser.close()  

python automation playwright playwright-python
1个回答
0
投票

这是由于浏览器在导航到下一个 URL 之前没有等待下载完成。通过添加等待条件来解决该问题,如下所示,

for url in urls:  
        page.goto(url)
        if(not flag):
            page.get_by_placeholder("User Email Address").fill("[email protected]")
            page.get_by_role("button", name="Continue").click()
            page.get_by_placeholder("Password").fill("test#12345")
            page.get_by_role("button", name="SSO Login").click()
            flag = True 

        # Wait for download to complete
        page.wait_for_download()

      
    browser.close()
© www.soinside.com 2019 - 2024. All rights reserved.