Selenium 4 File Upload and Download
In Selenium, file upload and download are common automation requirements.
-
File upload: Through
send_keys()method passes the file path to. -
File Download: By configuring the browser's download options and triggering the download operation, wait for the file download to complete.
-
Notes: Ensure the file path is correct, and adjust the wait time and browser configuration according to actual needs.
The following is a detailed explanation of file upload and download:
File upload
| Method | Description |
|---|---|
send_keys("file_path") |
Pass the file path to element to perform file upload. |
execute_script() |
If the file upload element is hidden, you can use JavaScript to make it visible. |
| Third-party tools (such as AutoIT) | If file upload is not done through , you may need to use third-party tools. |
File Download
| Method | Description |
|---|---|
| Configure Browser Download Options | Pass ChromeOptions or FirefoxOptions Set the download path, disable prompts, etc. |
click() |
Click the download button to trigger the file download. |
| Wait for the File Download to Complete | Use time.sleep() or loop to check whether the file has finished downloading. |
| Check the Downloaded File | Use os.listdir() Check the files in the download directory. |
Common Issues with File Upload and Download
| Problem | Solution |
|---|---|
| The File Upload Element Is Not Visible | Use JavaScript to make the element visible:driver.execute_script("arguments[0].style.display = 'block';", element) |
not" Element |
Use third-party tools (such as AutoIT or PyWin32) to simulate the file selection dialog. |
| Download Path Configuration Failed | Ensure the download path exists and is writable, and configure the browser options correctly. |
| The File Has Not Finished Downloading | Increase the wait time, or use a loop to check whether the file has finished downloading. |
File Upload Operation
File upload is one of the common features in web applications.
In Selenium, file upload is usually done throughelement.
The following are the steps to implement file upload:
Locate the File Upload Element: First, we need to locate the input element for file upload. Usually, this element is antag with the typefile.
Example
file_input
=
driver.
find_element
(
By.
XPATH
,
"//input[@type='file']"
)
Send the File Path: Once the file upload element is located, we can usesend_keys()method to send the absolute file path to the element.
Example
file_input.
send_keys
(
"/path/to/your/file.txt"
)
This will trigger the file selection dialog and automatically select the specified file.
Submitting a Form: In some cases, the form needs to be submitted after file upload. You can complete this by clicking the submit button.
Example
submit_button
=
driver.
find_element
(
By.
XPATH
,
"//input[@type='submit']"
)
submit_button.
click
(
)
Notes
- Ensure the file path is correct and the file exists.
- If the file upload element is hidden, you may need to use JavaScript to make it visible.
File Download Operation
File download is slightly more complex in Selenium because it involves the browser's download dialog.
The following are the steps to implement file download:
Set the Download Path: First, we need to set the browser's download path. This can be done by setting the browser's preferences.
Example
from
selenium.
webdriver
.
chrome
.
options
import
Options
chrome_options
=
Options
(
)
chrome_options.
add_experimental_option
(
"prefs"
,
{
"download.default_directory"
:
"/path/to/download/directory"
,
"download.prompt_for_download"
:
False
,
"download.directory_upgrade"
:
True
,
"safebrowsing.enabled"
:
True
}
)
driver
=
webdriver.
Chrome
(
options
=
chrome_options
)
This will set the browser's default download path and disable the download prompt.
Trigger the Download: Next, we need to find the link or button that triggers the download and click it.
Example
download_link
=
driver.
find_element
(
By.
XPATH
,
"//a[@id='downloadLink']"
)
download_link.
click
(
)
This will trigger the file download and automatically save it to the previously set download path.
Wait for the Download to Complete: To ensure the file download is complete, we can usetime.sleep()or a smarter waiting mechanism.
Example
import
time
time
.
sleep
(
10
)
Wait 10 seconds to ensure the file download is complete
Notes
- Ensure the download path is writable.
- If the downloaded file is large, you may need to increase the wait time.
Handle the Download Dialog
In some cases, the browser may pop up a download dialog asking the user whether to save the file. To handle this automatically, we can use the following methods:
Use Browser Preferences: As described above, you can disable the download dialog by setting the browser's preferences.
Example
chrome_options.
add_experimental_option
(
"prefs"
,
{
"download.prompt_for_download"
:
False
}
)
Use AutoIT or Similar Tools: If the download dialog cannot be disabled via browser preferences, you can use tools like AutoIT to automate the dialog.
Example
import
autoit
autoit.
win_wait_active
(
"File Download"
)
autoit.
control_click
(
"File Download"
,
"Button1"
)
This will automatically click the "Save" button in the download dialog.
Notes
- AutoIT scripts need to run in a Windows environment.
- Ensure the AutoIT script runs synchronously with the Selenium script.
Example Code
Here is a complete example showing how to configure Chrome browser's download options and download a file:
Example
from
selenium
import
webdriver
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
from
selenium.
webdriver
.
common
.
by
import
By
import
time
import
os
Set the Download Path
download_dir
=
"/path/to/download/directory"
Configure Chrome Options
chrome_options
=
webdriver.
ChromeOptions
(
)
prefs
=
{
"download.default_directory"
: download_dir
,
Set the Download Path
"download.prompt_for_download"
:
False
,
Disable Download Prompts
"download.directory_upgrade"
:
True
,
Allow Downloading to a Specified Directory
"safebrowsing.enabled"
:
True
Enable Safe Browsing
}
chrome_options.
add_experimental_option
(
"prefs"
,
prefs
)
Launch the browser
service
=
ChromeService
(
executable_path
=
"/path/to/chromedriver"
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
chrome_options
)
Open a web page
driver.
get
(
"https://www.example.com/download"
)
Click the Download Button
download_button
=
driver.
find_element
(
By.
ID
,
"download-button"
)
download_button.
click
(
)
Wait for the File Download to Complete
time
.
sleep
(
10
)
Adjust the Wait Time According to File Size
Check Whether the File Downloaded Successfully
files
=
os
.
listdir
(
download_dir
)
(
"Downloaded file list:"
,
files
)
Close the browser
driver.
quit
(
)