Bird
Raised Fist0
Selenium Pythontesting~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
File Upload 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?
Consider the following Selenium Python code that attempts to upload a file. What will be the result of executing this code?
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

browser = webdriver.Chrome()
browser.get('https://example.com/upload')
file_input = browser.find_element(By.ID, 'file-upload')
file_input.send_keys('/path/to/nonexistent/file.txt')
submit_button = browser.find_element(By.ID, 'submit-upload')
submit_button.click()

message = browser.find_element(By.ID, 'upload-message').text
print(message)
browser.quit()
AThe upload fails and an error message 'File not found' is displayed.
BThe file is uploaded successfully and a success message is printed.
CA NoSuchElementException is raised because the file input element is missing.
DA WebDriverException is raised due to invalid file path.
Attempts:
2 left
💡 Hint
Think about what happens when Selenium tries to upload a file that does not exist on the local system.
❓ locator
intermediate
1:30remaining
Which locator is best to find the file upload input element?
You want to locate the file upload input on a web page for Selenium automation. The input has the following HTML:
<input type="file" name="uploadFile" aria-label="Upload your document" id="file-upload">

Which locator is the most reliable and accessible?
ABy.XPATH, '//input[@aria-label="Upload your document"]'
BBy.NAME, 'uploadFile'
CBy.CSS_SELECTOR, 'input[type=file]'
DBy.ID, 'file-upload'
Attempts:
2 left
💡 Hint
IDs are unique and fast to locate elements.
❓ assertion
advanced
1:30remaining
Which assertion correctly verifies a successful file upload message?
After uploading a file, the page shows a message inside a div with id 'upload-status'. The expected message is 'Upload completed successfully'. Which assertion in Python unittest is correct?
Selenium Python
status_text = driver.find_element(By.ID, 'upload-status').text
Aself.assertIs(status_text, 'Upload completed successfully')
Bself.assertTrue('Upload completed successfully' in status_text)
Cself.assertEqual(status_text, 'Upload completed successfully')
Dself.assertAlmostEqual(status_text, 'Upload completed successfully')
Attempts:
2 left
💡 Hint
Check for exact string equality in the assertion.
🔧 Debug
advanced
2:00remaining
Why does this Selenium file upload test fail with ElementNotInteractableException?
The test code below tries to upload a file but fails with ElementNotInteractableException. What is the likely cause?
file_input = driver.find_element(By.ID, 'file-upload')
file_input.click()
file_input.send_keys('/path/to/file.txt')
AThe file path is incorrect, causing the exception.
BThe file input element is hidden or not visible, so it cannot be clicked or interacted with.
CThe click() method is unnecessary and causes the failure.
DThe driver is not initialized properly before locating the element.
Attempts:
2 left
💡 Hint
File inputs are often hidden and require direct send_keys without clicking.
❓ framework
expert
2:30remaining
In a Selenium test framework, which design best supports reusable file upload handling?
You are designing a Selenium test framework for multiple file upload scenarios. Which approach best supports maintainability and reusability?
ACreate a dedicated Page Object class with a method upload_file(file_path) that locates the file input and sends the file path.
BWrite file upload code directly inside each test case to keep tests independent.
CUse global variables for file paths and call send_keys directly in tests without abstraction.
DUse JavaScript executor to set file input value in every test without abstraction.
Attempts:
2 left
💡 Hint
Think about the Page Object Model pattern for reusable UI actions.

Practice

(1/5)
1. What is the primary Selenium method used to upload a file in a web form?
easy
A. send_keys() on the file input element
B. click() on the upload button
C. submit() on the form element
D. get() on the file path

Solution

  1. Step 1: Understand file upload in Selenium

    File upload is done by sending the file path to the file input element using send_keys().
  2. Step 2: Identify the correct method

    The send_keys() method inputs the file path string to the file input element, triggering the upload.
  3. Final Answer:

    send_keys() on the file input element -> Option A
  4. Quick Check:

    File upload = send_keys() [OK]
Hint: Use send_keys() on input type='file' to upload files [OK]
Common Mistakes:
  • Trying to click upload button instead of sending file path
  • Using submit() without sending file path
  • Using get() which loads URLs, not files
2. Which of the following is the correct way to upload a file using Selenium in Python?
easy
A. driver.find_element(By.ID, 'file').click('/path/to/file')
B. driver.find_element(By.ID, 'file').upload('/path/to/file')
C. driver.find_element(By.ID, 'file').send_keys('/path/to/file')
D. driver.find_element(By.ID, 'file').set_value('/path/to/file')

Solution

  1. Step 1: Recall Selenium file upload syntax

    The correct method to upload a file is send_keys() on the file input element.
  2. Step 2: Match the correct method call

    Only driver.find_element(By.ID, 'file').send_keys('/path/to/file') uses send_keys() correctly with the file path string.
  3. Final Answer:

    driver.find_element(By.ID, 'file').send_keys('/path/to/file') -> Option C
  4. Quick Check:

    send_keys() uploads file path [OK]
Hint: Use send_keys() with file path string on input element [OK]
Common Mistakes:
  • Using click() with file path argument
  • Using non-existent upload() or set_value() methods
  • Not passing absolute file path
3. What will be the result of this code snippet?
file_input = driver.find_element(By.NAME, 'upload')
file_input.send_keys('/home/user/document.txt')
print(file_input.get_attribute('value'))
medium
A. 'C:\\fakepath\\document.txt'
B. Empty string ''
C. Throws NoSuchElementException
D. Prints the file size

Solution

  1. Step 1: Understand send_keys effect on file input

    Sending a file path to the file input via send_keys() sets its value attribute to a browser-generated fake path 'C:\\fakepath\\document.txt' for security reasons.
  2. Step 2: Check get_attribute('value') output

    get_attribute('value') returns the fake path 'C:\\fakepath\\document.txt'.
  3. Final Answer:

    'C:\\fakepath\\document.txt' -> Option A
  4. Quick Check:

    send_keys() sets fake value attribute [OK]
Hint: get_attribute('value') shows fake path 'C:\\fakepath\\document.txt' [OK]
Common Mistakes:
  • Expecting the full file path instead of the fake path
  • Confusing attribute value with file content
  • Assuming print shows file size or content
4. You wrote this code to upload a file:
driver.find_element(By.CSS_SELECTOR, 'input[type="file"]').click()
driver.find_element(By.CSS_SELECTOR, 'input[type="file"]').send_keys('/path/to/file')

But the file is not uploaded. What is the likely problem?
medium
A. send_keys() requires a relative path, not absolute
B. Calling click() before send_keys() on file input blocks upload
C. CSS selector 'input[type="file"]' is invalid
D. File input elements cannot be automated

Solution

  1. Step 1: Analyze click() effect on file input

    Calling click() opens the OS file dialog, which Selenium cannot control, blocking automation.
  2. Step 2: Understand correct upload method

    Only send_keys() with the file path is needed; clicking before it can interfere.
  3. Final Answer:

    Calling click() before send_keys() on file input blocks upload -> Option B
  4. Quick Check:

    Don't click file input before send_keys() [OK]
Hint: Avoid click() on file input; just use send_keys() [OK]
Common Mistakes:
  • Clicking file input triggers OS dialog blocking automation
  • Using relative instead of absolute file paths
  • Assuming CSS selector is invalid without checking
5. You want to upload a file using Selenium but the file input element is hidden (style='display:none'). Which approach is best to handle this?
hard
A. Use send_keys() directly on hidden element
B. Upload file via browser DevTools network interception
C. Click the hidden element to open file dialog
D. Use JavaScript to remove 'display:none' and then send_keys()

Solution

  1. Step 1: Understand hidden file input behavior

    Hidden inputs cannot receive send_keys() because they are not interactable.
  2. Step 2: Use JavaScript to make element visible

    Removing 'display:none' via JavaScript makes the element interactable, allowing send_keys() to work.
  3. Step 3: Avoid clicking hidden elements or network hacks

    Clicking hidden elements fails; network interception is complex and not standard for file upload.
  4. Final Answer:

    Use JavaScript to remove 'display:none' and then send_keys() -> Option D
  5. Quick Check:

    Make hidden input visible before send_keys() [OK]
Hint: Make hidden input visible with JS before send_keys() [OK]
Common Mistakes:
  • Trying send_keys() on hidden element directly
  • Clicking hidden element expecting file dialog
  • Using complex network interception unnecessarily
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