Bird
Raised Fist0
Selenium Pythontesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of setting browser preferences in Selenium for file downloads?
easy
A. To specify the folder where downloaded files are saved automatically
B. To speed up the browser loading time
C. To change the browser's theme
D. To disable JavaScript on the page

Solution

  1. Step 1: Understand browser preferences role

    Browser preferences let you control how downloads behave, like where files save.
  2. Step 2: Identify the correct purpose

    Setting preferences to specify download folder avoids manual prompts and saves files automatically.
  3. Final Answer:

    To specify the folder where downloaded files are saved automatically -> Option A
  4. Quick Check:

    Download folder control = A [OK]
Hint: Remember: preferences set download folder and disable prompts [OK]
Common Mistakes:
  • Thinking preferences speed up browser loading
  • Confusing download folder with browser theme
  • Assuming preferences disable JavaScript
2. Which of the following is the correct way to set Firefox preferences in Selenium to automatically download PDF files to a folder without prompt?
easy
A. profile.set_preference('browser.download.folderList', '2')
B. profile.set_preference('browser.download.folderList', 2)
C. profile.set_preference('browser.download.folderList', True)
D. profile.set_preference('browser.download.folderList', None)

Solution

  1. Step 1: Recall Firefox preference for download folder

    The 'browser.download.folderList' preference expects an integer: 0 (desktop), 1 (default), 2 (custom folder).
  2. Step 2: Choose correct data type and value

    Using integer 2 sets a custom folder; string or other types cause errors.
  3. Final Answer:

    profile.set_preference('browser.download.folderList', 2) -> Option B
  4. Quick Check:

    Integer 2 for custom folder = B [OK]
Hint: Use integer 2, not string, for custom download folder [OK]
Common Mistakes:
  • Passing '2' as string instead of integer
  • Using boolean True instead of integer
  • Passing None which disables setting
3. Given this Selenium Python code snippet, what will be the output if the file is downloaded successfully?
import os
from selenium import webdriver

options = webdriver.FirefoxOptions()
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', 'application/pdf')
driver = webdriver.Firefox(firefox_profile=profile, options=options)
driver.get('http://example.com/sample.pdf')

file_path = '/tmp/downloads/sample.pdf'
print(os.path.exists(file_path))
medium
A. True
B. False
C. SyntaxError
D. FileNotFoundError

Solution

  1. Step 1: Analyze download setup

    Preferences set to save PDF automatically to '/tmp/downloads'.
  2. Step 2: Check file existence print

    After download, os.path.exists returns True if file is present.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    File exists after download = True [OK]
Hint: os.path.exists returns True if file is saved correctly [OK]
Common Mistakes:
  • Assuming print outputs file path string
  • Expecting False without download
  • Confusing syntax or runtime errors
4. Identify the error in this Selenium Python code for downloading a CSV file:
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(profile)
driver.get('http://example.com/data.csv')
medium
A. Missing options parameter in webdriver.Firefox()
B. Download folder path must be absolute
C. Incorrect argument name; should be firefox_profile=profile
D. Wrong MIME type for CSV files

Solution

  1. Step 1: Check webdriver.Firefox() usage

    Firefox expects profile passed as firefox_profile=profile, not just profile.
  2. Step 2: Confirm other settings

    MIME type 'text/csv' and folder path are correct; no options parameter needed here.
  3. Final Answer:

    Incorrect argument name; should be firefox_profile=profile -> Option C
  4. Quick Check:

    Use firefox_profile=profile argument = D [OK]
Hint: Pass profile with firefox_profile=profile keyword [OK]
Common Mistakes:
  • Passing profile as positional argument
  • Changing MIME type unnecessarily
  • Using relative folder path
5. You want to verify that a file named 'report.xlsx' has been downloaded successfully using Selenium Python. Which approach is best to confirm the download in an automated test?
hard
A. Check the page source for the file name
B. Check the browser's download history via Selenium API
C. Wait for an alert popup confirming download
D. Use os.path.exists() to check the file in the download folder

Solution

  1. Step 1: Understand Selenium's download verification limits

    Selenium cannot access browser download history or alerts for downloads.
  2. Step 2: Use file system check

    Checking file existence with os.path.exists() in the download folder confirms the file saved.
  3. Final Answer:

    Use os.path.exists() to check the file in the download folder -> Option D
  4. Quick Check:

    File system check confirms download = A [OK]
Hint: Verify downloads by checking file presence on disk [OK]
Common Mistakes:
  • Trying to read browser download history
  • Waiting for non-existent alert popups
  • Searching page source for downloaded file
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard