Open In App

Download File in Selenium Using Python

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Selenium

Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e. Python, Java, C#, etc. We will be working with Python. Selenium Tutorial covers all topics such as– WebDriver, WebElement, Unit Testing with selenium.  In this article, we are going to see to Download the File From Web Page Using Selenium in Python.

For Downloading the File, we will use the click() method. Here our automation we will download a generated text file.

Download File in Selenium Using Python

Follow these steps –

  • Enter data
  • Click on generate, it will generate a text file
  • Click on download, it will download the text file

Here we will use id for entering and generating the text file.

When a file is generated it will give a download option, click on it, the download will start.

Approach:

  • Import module.
  • Make an object for chromedriver.
  • Get URL with get() methods.
  • Create automation text.
  • Create link automation for downloading.

Below is the full implementation:

Python3




# Import Module
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
 
# Open Chrome
driver = webdriver.Chrome(
    'C:/Users/HP/Desktop/Drivers/chromedriver_win32/chromedriver.exe')
 
# Open URL
driver.get(
 
# Enter text
driver.find_element_by_id('textbox').send_keys("Hello world")
 
# Generate Text File
driver.find_element_by_id('createTxt').click()
 
# Click on Download Button
driver.find_element_by_id('link-to-download').click()


Output:

Similarly, we can download a PDF file or any other document. 


Last Updated : 28 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads