Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the download directory in Selenium WebDriver.
Selenium Python
options = webdriver.ChromeOptions() options.add_experimental_option('prefs', {'download.default_directory': [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a boolean or number instead of a string path.
Not quoting the path string.
✗ Incorrect
The download.default_directory preference must be set to the path where files should be saved.
2fill in blank
mediumComplete the code to wait until the file is downloaded by checking the file existence.
Selenium Python
import os import time file_path = '/path/to/downloads/file.txt' while not os.path.exists([1]): time.sleep(1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing just the filename instead of full path.
Passing the module os.path instead of a string.
✗ Incorrect
We must check the full file path variable to detect if the file exists.
3fill in blank
hardFix the error in the code that sets Chrome options for automatic download without prompt.
Selenium Python
options = webdriver.ChromeOptions() options.add_experimental_option('prefs', [1]) driver = webdriver.Chrome(options=options)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list or tuple instead of a dictionary.
Passing just a boolean instead of a dictionary.
✗ Incorrect
The prefs argument must be a dictionary with keys and values, not a list or tuple.
4fill in blank
hardFill both blanks to create a function that waits for a file to be fully downloaded by checking file size stability.
Selenium Python
def wait_for_download(file_path): import os, time last_size = -1 while True: current_size = os.path.getsize([1]) if current_size == last_size: break last_size = current_size time.sleep([2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable name for file path.
Using too short or missing sleep causing high CPU usage.
✗ Incorrect
We check the size of the file_path and wait 1 second between checks to confirm download completion.
5fill in blank
hardFill all three blanks to configure Firefox profile for automatic file download of PDFs without prompt.
Selenium Python
from selenium import webdriver profile = webdriver.FirefoxProfile() profile.set_preference('browser.download.folderList', [1]) profile.set_preference('browser.download.dir', [2]) profile.set_preference('browser.helperApps.neverAsk.saveToDisk', [3]) driver = webdriver.Firefox(firefox_profile=profile)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using folderList 1 or 0 which do not allow custom folder.
Not specifying the correct MIME type string.
✗ Incorrect
Setting folderList to 2 means custom folder, then specify the path and MIME type for PDFs to download automatically.