我尝试使用 Playwrgith 自动下载 PDF 文件,我使用 Selenium 工作的代码,但 Playwright 中的一些功能引起了我的注意。文档的真正问题
没有帮助。当我点击下载时,我得到这个:
而且我无法更改下载目录,当浏览器/上下文关闭时,它也会删除“文件”。使用 Playwright 可以实现不错的下载自动化?
代码:
def run(playwright):
browser = playwright.chromium.launch(headless=False)
context = browser.new_context(accept_downloads=True)
# Open new page
page = context.new_page()
# Go to http://xcal1.vodafone.co.uk/
page.goto("http://xcal1.vodafone.co.uk/")
# Click text=Extra Small File 5 MB A high quality 5 minute MP3 music file 30secs @ 2 Mbps 10s >> img
with page.expect_download() as download_info:
page.click("text=Extra Small File 5 MB A high quality 5 minute MP3 music file 30secs @ 2 Mbps 10s >> img")
download = download_info.value
path = download.path()
download.save_as(path)
print(path)
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)