0
0
Selenium Pythontesting~10 mins

File download handling in Selenium Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
ANone
BTrue
C'/path/to/downloads'
D8080
Attempts:
3 left
💡 Hint
Common Mistakes
Using a boolean or number instead of a string path.
Not quoting the path string.
2fill in blank
medium

Complete 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'
Afile_path
B'file.txt'
Cfile
Dos.path
Attempts:
3 left
💡 Hint
Common Mistakes
Passing just the filename instead of full path.
Passing the module os.path instead of a string.
3fill in blank
hard

Fix 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'
AFalse
B['download.prompt_for_download', False]
C('download.prompt_for_download', False)
D{'download.prompt_for_download': False}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list or tuple instead of a dictionary.
Passing just a boolean instead of a dictionary.
4fill in blank
hard

Fill 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'
Afile_path
Bfile
C1
D0.1
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable name for file path.
Using too short or missing sleep causing high CPU usage.
5fill in blank
hard

Fill 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'
A2
B'/path/to/downloads'
C'application/pdf'
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using folderList 1 or 0 which do not allow custom folder.
Not specifying the correct MIME type string.