Challenge - 5 Problems
File Upload Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Think about what happens when Selenium tries to upload a file that does not exist on the local system.
✗ Incorrect
Selenium's send_keys method for file inputs requires the file to exist on the local machine. If the file path is invalid or the file does not exist, the upload will fail and the web page typically shows an error message like 'File not found'. No exceptions are raised by Selenium itself in this case.
❓ locator
intermediate1: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:
Which locator is the most reliable and accessible?
<input type="file" name="uploadFile" aria-label="Upload your document" id="file-upload">
Which locator is the most reliable and accessible?
Attempts:
2 left
💡 Hint
IDs are unique and fast to locate elements.
✗ Incorrect
Using By.ID with 'file-upload' is the best locator because IDs are unique on the page and fast for Selenium to find. While other locators work, they may be less specific or slower.
❓ assertion
advanced1: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').textAttempts:
2 left
💡 Hint
Check for exact string equality in the assertion.
✗ Incorrect
assertEqual compares two values for exact equality, which is appropriate here. assertTrue with 'in' would pass if the message is a substring, which may be less strict. assertIs checks object identity, not string equality. assertAlmostEqual is for numbers.
🔧 Debug
advanced2: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')Attempts:
2 left
💡 Hint
File inputs are often hidden and require direct send_keys without clicking.
✗ Incorrect
ElementNotInteractableException occurs when the element is present but not visible or interactable. File inputs are often hidden for styling, so clicking them fails. The correct approach is to send_keys directly without clicking.
❓ framework
expert2: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?
Attempts:
2 left
💡 Hint
Think about the Page Object Model pattern for reusable UI actions.
✗ Incorrect
Using a Page Object class with a method for file upload encapsulates the locator and action, making tests cleaner and easier to maintain. Writing code directly in tests or using globals reduces reusability and increases duplication. Using JavaScript executor bypasses standard interaction and is less maintainable.