Alex Dicc
Alex Dicc

Reputation: 11

download csv generated file with Playwright

I m trying to download a file from a web page using playwright. The file is a csv file generated from the page to download a list of customers. So far and with the current code i ve written i download the file but i cannot access it. I ve done my research and from what i ve found playwright downloads the file momenterily in the downloads of chromium and then it gets deleted when the browser closes.

I ve found some solutions but none seems to work with the way i ve used playwright. I see the words "await" and "const" all the time , but i don't have them in my code.

the code is as follows:

from playwright.sync_api import sync_playwright
import datetime
from datetime import date,timedelta

link = 'link of the page that has the info'
today= date.today()
dtgb= datetime.timedelta(60) #days_to_go_back
past_date= today-dtgb
past_date= past_date.strftime("%d/%m/%Y")
today= today.strftime("%d/%m/%Y")


with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, slow_mo=10000)
    page = browser.new_page()
    page.goto(link)
    page.click('div [tabindex="4"]')
    page.fill('input#i0116', 'username')
    page.click('input#idSIButton9')
    page.fill('input#i0118', 'password')
    page.click('input#idSIButton9')
    page.click('input#idSIButton9')
    page.goto(link)
    page.is_visible('div.form-body')
    page.fill('input[name="DateFrom"]',past_date)
    page.fill('input[name="DateTo"]', today)
    page.click('button[type=submit]')
    page.click('button[title=export]')

after the export button, the file gets downloaded, browser closes and i don't have the file.

the button html is:

<button ng-if="$ctrl.results.length" class="btn btn-success ng-scope" title="export" ng-click="$ctrl.createCSV($ctrl.serverResults)" style="">

<i class="fa fa-download">

</i>

</button>

any help is welcome.

*ignore the slow _mo=10000, i'm very very new and want everything to go slow so i can see what's going on.

Upvotes: 1

Views: 2454

Answers (1)

Douglas
Douglas

Reputation: 11

This here worked for me:

with page.expect_download() as download_info:    
    page.goto("Link")
    page.click('xpath=button')
download = download_info.value
path = download.path()
download.save_as("filename.csv")
print(path)

#---------------------
context.close()
browser.close()

I hope it helps you!

Upvotes: 1

Related Questions