0
0
Selenium Javatesting~20 mins

File upload (sendKeys to input) in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 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.