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 result of this Selenium Java code snippet?
Consider the following Selenium Java code that attempts to upload a file using sendKeys on an input element of type file. What will be the output or behavior when this code runs?
Selenium Java
WebElement uploadInput = driver.findElement(By.id("fileUpload")); uploadInput.sendKeys("C:\\Users\\Test\\Documents\\file.txt"); System.out.println("File path sent to input.");
Attempts:
2 left
💡 Hint
Remember that sendKeys on a file input element sets the file path directly without opening dialogs.
✗ Incorrect
In Selenium, sendKeys on an input element of type 'file' sets the file path directly, simulating file selection. No dialog opens, and no exceptions occur if the element is found and the path is valid.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies the file upload input value after sendKeys?
After sending the file path to the input element, which assertion correctly checks that the input's value ends with the uploaded file name "file.txt"?
Selenium Java
WebElement uploadInput = driver.findElement(By.id("fileUpload")); uploadInput.sendKeys("C:\\Users\\Test\\Documents\\file.txt");
Attempts:
2 left
💡 Hint
The file input's value attribute contains the file path or name, not the visible text.
✗ Incorrect
The value attribute of a file input contains the file path or name after sendKeys. Using getText() returns empty string for input elements. Checking 'src' attribute is irrelevant here.
❓ locator
advanced2:00remaining
Which locator is best to find a file upload input with label text "Upload your document"?
You want to locate the file input element associated with the label text "Upload your document". Which locator is the best practice to find the input element reliably?
Attempts:
2 left
💡 Hint
Use label text to find the input element it describes for better accessibility and reliability.
✗ Incorrect
Option A uses the label text to find the input element next to it, ensuring the correct file input is selected even if multiple exist. Other options may be ambiguous or rely on IDs that may not exist.
🔧 Debug
advanced2:00remaining
Why does this file upload test fail with ElementNotInteractableException?
Given this code snippet, the test fails with ElementNotInteractableException. What is the most likely cause?
Selenium Java
WebElement uploadInput = driver.findElement(By.id("hiddenFileInput")); uploadInput.sendKeys("C:\\Users\\Test\\Documents\\file.txt");
Attempts:
2 left
💡 Hint
Check if the input element is visible and enabled before sending keys.
✗ Incorrect
ElementNotInteractableException occurs when the element is present but not visible or interactable, such as when it is hidden by CSS. sendKeys requires the element to be visible.
❓ framework
expert3:00remaining
How to design a reusable file upload method in Selenium Java?
You want to create a reusable method in your Selenium Java test framework to upload files by sending file paths to input elements. Which method signature and implementation is best practice?
Attempts:
2 left
💡 Hint
The method should accept a locator and file path, find the element, and send keys without unnecessary steps.
✗ Incorrect
Option B cleanly accepts a locator and file path, finds the element, and sends the file path. Option B assumes only one input exists, which is fragile. Option B clicks before sendKeys, which is unnecessary and may cause errors. Option B calls clear() on file input, which is invalid and causes exception.