Bird
0
0

Which of the following is the correct syntax to upload a file located at C:\files\test.txt using Selenium WebDriver in Java?

easy📝 Syntax Q12 of 15
Selenium Java - Handling Form Elements
Which of the following is the correct syntax to upload a file located at C:\files\test.txt using Selenium WebDriver in Java?
Adriver.findElement(By.id("fileUpload")).sendKeys("C:\\files\\test.txt");
Bdriver.findElement(By.id("fileUpload")).click().sendKeys("C:/files/test.txt");
Cdriver.findElement(By.id("fileUpload")).sendKeys("test.txt");
Ddriver.findElement(By.id("fileUpload")).setText("C:\files\test.txt");
Step-by-Step Solution
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]
Quick Trick: Escape backslashes in Windows paths and use sendKeys [OK]
Common Mistakes:
MISTAKES
  • Using relative file name without full path
  • Using click() before sendKeys
  • Using non-existent setText() method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes