Download a File using Playwright Python

Tutorial 13​​ ​​ Download a File using​​ Playwright​​ Python​​ 

What you will Learn​​ in this​​ blog:

  • Download a file using Playwright python

  • File download path

  • Code snippets

Download a file using Playwright python

Let us​​ now see how to​​ download a file using Playwright python.

We will download a file from​​ the​​ test website page

https://the-internet.herokuapp.com/download​​ 

Let us inspect file​​ ‘LambdaTest.txt’​​ 

The​​ expect_download()​​ method​​ can be used to ensure that playwright waits for the download to complete ​​ 

We​​ will now use the​​ with-as​​ block and within this block we will perform​​ the ‘click’ operation to download the file

To get​​ the​​ url of​​ the file​​ that​​ gets downloaded, we can use the getter method ‘url’​​ 

Thus we have

We are printing the url using line#15.

Save and execute, we get the file url.​​ 

The file url (alongwith the filename) can be seen

Also notice that the file gets downloaded​​ 


So this is how​​ we can download a file using Playwright python.

File download path

To know the path where the file gets downloaded during runtime, there is a ‘path’ method that we can use​​ 

Save and execute.

Notice below the temporary path getting printed in console

To download a file at specific location, we will first fetch the current working directory using​​ getcwd()​​ command in the​​ os​​ package

Store the path in a variable

Create a new directory under working dir

Use the​​ join​​ function to join the 2 directories

Store this joined path in a var

Finally, let us save the file in downloads dir

Save and execute.​​ 

The file gets downloaded (can refresh the project if you don’t see the file)


Code snippet

import​​ os

from​​ playwright.sync_api​​ import​​ Playwright,​​ sync_playwright,​​ expect


def​​ run(playwright: Playwright) ->​​ None:
 ​​ ​​ ​​​​ browser = playwright.chromium.launch(
headless=False)
 ​​ ​​ ​​​​ context = browser.new_context()


 ​​ ​​ ​​​​ page = context.new_page()

 ​​ ​​ ​​​​ page.goto(
"https://the-internet.herokuapp.com/download")

 ​​ ​​ ​​​​ 
with​​ page.expect_download()​​ as​​ d_file:
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ page.locator(
"//a[text()='LambdaTest.txt']").click()

 ​​ ​​ ​​​​ 
f_url​​ = d_file.value.url
 ​​ ​​ ​​​​ 
#print(f_url)
 ​​ ​​ ​​​​ 
print(d_file.value.path())

 ​​ ​​ ​​​​ page.wait_for_timeout(
4000)

 ​​ ​​ ​​​​ current_working_dir = os.getcwd()


 ​​ ​​ ​​​​ file_path = os.path.join(current_working_dir
,​​ "downloads/file.txt")

 ​​ ​​ ​​​​ d_file.value.save_as(file_path)


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



with​​ sync_playwright()​​ as​​ playwright:
 ​​ ​​ ​​​​ run(playwright)

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024