Today, Python is becoming increasingly popular for automating tasks in web applications. In this blog post, we will see how to use a selenium Python script to download files from a web application.
The selenium Python script we will be using will work on any web application that has a “download” link. For our example, we will be using the Python scripts module from the SeleniumHQ GitHub repository. This module provides a convenient way to access and manipulate the files in a web application.
First, we need to install the SeleniumHQ/selenium Python package. The easiest way to do this is using pip:
pip install selenium
Once Selenium is installed, we can write our Python script. The script below will launch a web browser, navigate to the SeleniumHQ downloads page, click on the link to download the selenium-python-2.xx.zip file, and then save it to the current directory.
from selenium import webdriver
# Path to chromedriver executable – change this if necessary
chromedriver_path = “/path/to/chromedriver”
# URL of page to download file from
download_url = “http://selenium-release.storage.googleapis.com/index.html”
# Path to file on local filesystem that we want to download
file_path = “/path/to/file”
# Start chrome driver and navigate to download page
driver = webdriver.Chrome(chromedriver_path) driver.get(download_url)
# Find the “Download” link and click it
driver.find_element_by_link_text(“Download”).click()
# Wait for the browser to start downloading the file
while not os.path .exists(file_path): time .sleep(1)
# When the download is complete, close the browser