Bird
Raised Fist0
Selenium Javatesting~20 mins

File upload (sendKeys to input) in Selenium Java - 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 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.");
AThe file path is sent to the input element, and the file is ready for upload. The console prints: File path sent to input.
BThrows NoSuchElementException because the input element is not found.
CThrows InvalidArgumentException because sendKeys does not accept file paths.
DThe file dialog window opens for manual file selection.
Attempts:
2 left
💡 Hint
Remember that sendKeys on a file input element sets the file path directly without opening dialogs.
❓ assertion
intermediate
2: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");
AassertEquals(uploadInput.getText(), "file.txt");
BassertNotNull(uploadInput.getAttribute("src"));
CassertTrue(uploadInput.getAttribute("value").endsWith("file.txt"));
DassertFalse(uploadInput.getAttribute("value").contains("file.txt"));
Attempts:
2 left
💡 Hint
The file input's value attribute contains the file path or name, not the visible text.
❓ locator
advanced
2: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?
ABy.xpath("//label[text()='Upload your document']/following-sibling::input[@type='file']")
BBy.cssSelector("input[type='file']")
CBy.id("upload")
DBy.xpath("//input[@type='file' and @name='uploadFile']")
Attempts:
2 left
💡 Hint
Use label text to find the input element it describes for better accessibility and reliability.
🔧 Debug
advanced
2: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");
AsendKeys cannot be used on input elements of type file.
BThe file path is incorrect, causing the exception.
CThe driver has lost focus on the browser window.
DThe input element is hidden or has CSS display:none, so it cannot be interacted with.
Attempts:
2 left
💡 Hint
Check if the input element is visible and enabled before sending keys.
❓ framework
expert
3: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?
A
public void uploadFile(String filePath) {
  WebElement input = driver.findElement(By.cssSelector("input[type='file']"));
  input.sendKeys(filePath);
}
B
public void uploadFile(By locator, String filePath) {
  WebElement input = driver.findElement(locator);
  input.sendKeys(filePath);
}
C
public void uploadFile(WebElement input, String filePath) {
  input.click();
  input.sendKeys(filePath);
}
D
public void uploadFile(By locator, String filePath) {
  WebElement input = driver.findElement(locator);
  input.clear();
  input.sendKeys(filePath);
}
Attempts:
2 left
💡 Hint
The method should accept a locator and file path, find the element, and send keys without unnecessary steps.

Practice

(1/5)
1. What is the correct way to upload a file using Selenium WebDriver in Java?
easy
A. Use getText() on the file input element to upload the file.
B. Use click() on the file input element and then type the file name.
C. Use executeScript to set the file content directly.
D. Use sendKeys on the file input element with the full file path.

Solution

  1. Step 1: Understand file upload in Selenium

    File upload requires sending the full file path to the file input element using sendKeys.
  2. Step 2: Identify correct method usage

    Only sendKeys accepts the file path string to upload the file; other methods do not perform upload.
  3. Final Answer:

    Use sendKeys on the file input element with the full file path. -> Option D
  4. Quick Check:

    File upload = sendKeys with full path [OK]
Hint: Always use sendKeys with full file path to upload files [OK]
Common Mistakes:
  • Trying to click and type file name manually
  • Using getText() which only reads text
  • Using executeScript to set file content
2. Which of the following is the correct syntax to upload a file located at C:\files\test.txt using Selenium WebDriver in Java?
easy
A. driver.findElement(By.id("fileUpload")).sendKeys("C:\\files\\test.txt");
B. driver.findElement(By.id("fileUpload")).click().sendKeys("C:/files/test.txt");
C. driver.findElement(By.id("fileUpload")).sendKeys("test.txt");
D. driver.findElement(By.id("fileUpload")).setText("C:\files\test.txt");

Solution

  1. Step 1: Use sendKeys with full absolute path

    The file input requires the full absolute path with escaped backslashes in Java strings.
  2. Step 2: Check syntax correctness

    driver.findElement(By.id("fileUpload")).sendKeys("C:\\files\\test.txt"); correctly escapes backslashes as double backslashes and uses sendKeys on the element.
  3. Final Answer:

    driver.findElement(By.id("fileUpload")).sendKeys("C:\\files\\test.txt"); -> Option A
  4. Quick Check:

    Escape backslashes and use sendKeys [OK]
Hint: Escape backslashes in Windows paths and use sendKeys [OK]
Common Mistakes:
  • Using relative file name without full path
  • Using click() before sendKeys
  • Using non-existent setText() method
3. What will be the result of the following code snippet if the file path is correct and the element is found?
WebElement upload = driver.findElement(By.name("uploadFile"));
upload.sendKeys("/home/user/docs/sample.pdf");
String value = upload.getAttribute("value");
System.out.println(value);
medium
A. Prints the full file path: /home/user/docs/sample.pdf
B. Prints the file name only: sample.pdf
C. Throws NoSuchElementException
D. Prints an empty string

Solution

  1. Step 1: Understand getAttribute("value") on file input

    After sendKeys, the value attribute usually contains only the file name, not full path, for security reasons.
  2. Step 2: Analyze output of System.out.println(value)

    The printed output will be the file name part: sample.pdf, not the full path or empty string.
  3. Final Answer:

    Prints the file name only: sample.pdf -> Option B
  4. Quick Check:

    getAttribute("value") returns file name [OK]
Hint: getAttribute("value") on file input returns file name only [OK]
Common Mistakes:
  • Expecting full file path printed
  • Assuming empty string is returned
  • Confusing NoSuchElementException with normal behavior
4. You wrote this code to upload a file:
driver.findElement(By.id("fileInput")).sendKeys("docs/report.pdf");

But the file is not uploading. What is the most likely reason?
medium
A. The file path is relative; sendKeys requires the full absolute path.
B. sendKeys cannot be used on file input elements.
C. The locator By.id("fileInput") is invalid syntax.
D. You must call click() before sendKeys to upload.

Solution

  1. Step 1: Check file path usage in sendKeys

    sendKeys requires the full absolute file path, not a relative path like "docs/report.pdf".
  2. Step 2: Verify other options

    sendKeys works on file inputs; locator syntax is correct; click() before sendKeys is not needed.
  3. Final Answer:

    The file path is relative; sendKeys requires the full absolute path. -> Option A
  4. Quick Check:

    Use absolute path in sendKeys [OK]
Hint: Always provide absolute file path to sendKeys [OK]
Common Mistakes:
  • Using relative file paths
  • Thinking click() is required before sendKeys
  • Misunderstanding locator syntax
5. You want to verify that a file upload succeeded after using sendKeys. Which approach is best to confirm the upload in your Selenium test?
hard
A. Use getText() on the file input element to see the file name.
B. Check the file input's value attribute for the full file path.
C. Check for a success message or confirmation element on the page after upload.
D. Assume upload succeeded if no exception was thrown during sendKeys.

Solution

  1. Step 1: Understand verification after file upload

    File input's value attribute does not reliably confirm upload; success message or page change is best evidence.
  2. Step 2: Evaluate options for confirming upload

    Checking UI confirmation or success message ensures upload succeeded; relying on no exception or input value is unreliable.
  3. Final Answer:

    Check for a success message or confirmation element on the page after upload. -> Option C
  4. Quick Check:

    Verify upload by success message presence [OK]
Hint: Confirm upload by checking success message or page change [OK]
Common Mistakes:
  • Relying on input value attribute for confirmation
  • Assuming no exception means success
  • Using getText() on file input which returns empty
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