0
0
Selenium Pythontesting~20 mins

File download handling in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Download Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium Python code snippet for file download?
Consider the following Selenium Python code that sets up a Firefox profile to automatically download PDF files without showing the download dialog. What will be the value of download_dir after the code runs?
Selenium Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
profile = webdriver.FirefoxProfile()
download_dir = '/tmp/downloads'
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.dir', download_dir)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
profile.set_preference('pdfjs.disabled', True)
driver = webdriver.Firefox(firefox_profile=profile, options=options)

print(download_dir)
A/home/user/downloads
Bbrowser.download.dir
C/tmp/downloads
DNone
Attempts:
2 left
💡 Hint
Look at the variable assigned to download_dir and what is printed.
assertion
intermediate
1:30remaining
Which assertion correctly verifies a file was downloaded?
You want to check if a file named 'report.pdf' exists in the download directory after triggering a download in Selenium Python. Which assertion is correct?
Selenium Python
import os
file_path = '/tmp/downloads/report.pdf'

# Which assertion below correctly checks the file exists?
Aassert os.path.isfile(file_path)
Bassert os.path.exists(file_path) == False
Cassert os.path.isdir(file_path)
Dassert file_path in os.listdir('/tmp/downloads') == False
Attempts:
2 left
💡 Hint
Use the function that checks if a path is a file.
🔧 Debug
advanced
2:30remaining
Why does this Selenium Python code fail to download the file automatically?
This code is intended to download CSV files automatically without a dialog, but the download dialog still appears. What is the likely cause?
Selenium Python
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.dir', '/tmp/downloads')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('http://example.com/download.csv')
AFirefoxProfile is deprecated and ignored in recent Selenium versions
BIncorrect MIME type in 'neverAsk.saveToDisk' preference
CMissing setting to disable built-in PDF viewer
DDownload directory path must be absolute
Attempts:
2 left
💡 Hint
Check if FirefoxProfile is still supported in recent Selenium versions.
🧠 Conceptual
advanced
2:00remaining
What is the best way to wait for a file download to complete in Selenium Python?
You trigger a file download in Selenium Python. The file may take some seconds to appear in the download folder. Which approach best waits for the download to finish?
AUse time.sleep(10) to wait a fixed time before checking the file
BCheck the browser console logs for download completion message
CUse Selenium explicit waits on the download button element
DPoll the download folder checking for the file existence and size stability
Attempts:
2 left
💡 Hint
Waiting for file size to stop changing is a reliable method.
framework
expert
3:00remaining
How to configure ChromeDriver in Selenium Python to download files without prompt?
You want to configure ChromeDriver to download files automatically to '/tmp/downloads' without showing the download prompt. Which code snippet correctly sets this up?
A
from selenium import webdriver
options = webdriver.ChromeOptions()
options.set_preference('download.default_directory', '/tmp/downloads')
options.set_preference('download.prompt_for_download', False)
driver = webdriver.Chrome(options=options)
B
from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {'download.default_directory': '/tmp/downloads', 'download.prompt_for_download': False}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options=options)
C
from selenium import webdriver
prefs = {'download.default_directory': '/tmp/downloads', 'download.prompt_for_download': False}
driver = webdriver.Chrome(prefs=prefs)
D
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--download.default_directory=/tmp/downloads')
driver = webdriver.Chrome(options=options)
Attempts:
2 left
💡 Hint
ChromeOptions uses add_experimental_option to set preferences.