Skip to content
Related Articles

Related Articles

Improve Article

Download File in Selenium Using Python

  • Difficulty Level : Hard
  • Last Updated : 04 Jan, 2021
Geek Week

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




# 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. 

 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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :