This test opens a webpage with a file upload form, uploads a file by sending its path to the input, clicks submit, and checks if a success message appears.
Make sure the file path is correct and accessible by the test machine.
Do not try to click the file input; sendKeys works directly on it.
File input elements must be visible or interactable for sendKeys to work.
Summary
Use sendKeys on the file input element to upload files in tests.
Always provide the full file path as a string.
Check for success messages or page changes after upload.
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
Step 1: Understand file upload in Selenium
File upload requires sending the full file path to the file input element using sendKeys.
Step 2: Identify correct method usage
Only sendKeys accepts the file path string to upload the file; other methods do not perform upload.
Final Answer:
Use sendKeys on the file input element with the full file path. -> Option D
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
Step 1: Use sendKeys with full absolute path
The file input requires the full absolute path with escaped backslashes in Java strings.
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.
Final Answer:
driver.findElement(By.id("fileUpload")).sendKeys("C:\\files\\test.txt"); -> Option A
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
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.
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.
Final Answer:
Prints the file name only: sample.pdf -> Option B
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