Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to upload a file by sending the file path to the input element.
Selenium Java
WebElement uploadInput = driver.findElement(By.id("fileUpload")); uploadInput.[1]("C:/Users/Example/file.txt");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using click() instead of sendKeys() to upload a file.
Trying to use getText() which only reads text, not uploads files.
✗ Incorrect
The sendKeys method is used to simulate typing the file path into the file input element, which triggers the file upload.
2fill in blank
mediumComplete the code to locate the file input element by its name attribute.
Selenium Java
WebElement uploadInput = driver.findElement(By.[1]("uploadFile"));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using By.id when the element does not have an id attribute.
Using By.className or By.tagName which do not match the element uniquely.
✗ Incorrect
The By.name locator finds elements by their name attribute, which is 'uploadFile' in this case.
3fill in blank
hardFix the error in the code to correctly upload a file using sendKeys.
Selenium Java
driver.findElement(By.id("fileInput")).[1]("/path/to/file.pdf");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using click() which only clicks the element but does not upload.
Using getAttribute() which reads attributes but does not upload.
✗ Incorrect
sendKeys is the correct method to input the file path into the file input element for uploading.
4fill in blank
hardFill both blanks to locate the file input by CSS selector and upload the file.
Selenium Java
WebElement fileInput = driver.findElement(By.[1]("[2]")); fileInput.sendKeys("C:/files/test.png");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using xpath locator but providing a CSS selector string.
Using class selector when the element has an id.
✗ Incorrect
By.cssSelector("#fileUpload") locates the element with id 'fileUpload'. Then sendKeys uploads the file.
5fill in blank
hardFill all three blanks to find the file input by XPath, clear it, and upload a file.
Selenium Java
WebElement input = driver.findElement(By.[1]("[2]")); input.[3](); input.sendKeys("/home/user/image.jpg");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using click() instead of clear() before sendKeys.
Using incorrect XPath expression.
✗ Incorrect
By.xpath("//input[@type='file']") finds the file input element. clear() empties any existing value before sending the new file path.