Skip to content
Related Articles

Related Articles

Download File in Selenium Using Python
  • Last Updated : 04 Jan, 2021

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.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output:

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :