Bird
0
0

What will be the result of the following code snippet if the file path is correct and the element is found?

medium📝 Predict Output Q13 of 15
Selenium Java - Handling Form Elements
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);
APrints the full file path: /home/user/docs/sample.pdf
BPrints the file name only: sample.pdf
CThrows NoSuchElementException
DPrints an empty string
Step-by-Step Solution
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]
Quick Trick: getAttribute("value") on file input returns file name only [OK]
Common Mistakes:
MISTAKES
  • Expecting full file path printed
  • Assuming empty string is returned
  • Confusing NoSuchElementException with normal behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes