Download multiple PDF files with Python Playwright

54 views Asked by At

I'm trying to automate a PDF download with Playwright in Python from this website: https://www4.trf5.jus.br/diarioeletinternet/paginas/consultas/consultaDiario.faces.

website print

In this example, the script should download the 19 PDFs. But it only downloads 1 and stops.

Here's the script:

from playwright.sync_api import sync_playwright, Playwright

with sync_playwright() as p:
    nav = p.chromium.launch(headless=False)

    page = nav.new_page(accept_downloads=True) 
    
    page.goto(
    'https://www4.trf5.jus.br/diarioeletinternet/paginas/consultas/consultaDiario.faces;jsessionid=A7EF0E11BE9943A9942B54CE45F7C7F9'
    )

    page.locator("[name='frmVisao:orgao']").select_option('SeçãoJudiciária do Sergipe')
    page.locator("[name='frmVisao:edicao']").select_option('Administrativo')
    page.locator("[name='frmVisao:periodo']").select_option('2021')
    page.wait_for_function("""
        document
          .querySelectorAll('[id="frmVisao:meses"] option')
          .length > 11
    """)
    page.locator('xpath=//*[@id="frmVisao:meses"]').select_option('03')
    page.locator('xpath=//*[@id="frmVisao:j_id48"]').click()
    page.locator('xpath=//*[@id="frmPesquisa:quantidadeRegistros"]').select_option('50')
    rows = page.query_selector_all(".rich-table tbody tr td:nth-child(4)")

    for row in rows:
        with page.expect_download() as download_info:
            row.click()
        
        download = download_info.value
        download.save_as(download.suggested_filename)
    
    page.close()
    nav.close()

Where's the error?

0

There are 0 answers