0
0
Selenium Javatesting~5 mins

File upload (sendKeys to input) in Selenium Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the sendKeys method do when used on a file input element in Selenium?
It simulates typing the file path into the file input field, effectively selecting the file to upload without opening the file dialog.
Click to reveal answer
beginner
Why can't Selenium interact with native OS file dialogs directly?
Because Selenium controls the browser, not the operating system, so it cannot automate native dialogs like file pickers.
Click to reveal answer
beginner
Show a simple Java Selenium code snippet to upload a file using sendKeys.
WebElement uploadInput = driver.findElement(By.id("file-upload")); uploadInput.sendKeys("C:\\Users\\User\\Documents\\file.txt");
Click to reveal answer
intermediate
What is a best practice for locating the file input element for uploading files?
Use a unique and stable locator like id or name attributes to avoid flaky tests.
Click to reveal answer
intermediate
What should you verify after uploading a file using sendKeys in a test?
Verify that the file name appears in the UI or that the upload was successful by checking for confirmation messages or file presence.
Click to reveal answer
Which Selenium method is used to upload a file by setting the file path directly?
AgetText
Bclick
CsendKeys
Dsubmit
Why is it better to use id or name attributes to locate the file input element?
AThey open the file dialog automatically
BThey make the test run faster
CThey allow interacting with OS dialogs
DThey are usually unique and stable locators
What happens if you try to use click() on a file input element to upload a file in Selenium?
AIt opens the OS file dialog, which Selenium cannot control
BIt uploads the file automatically
CIt throws an error
DIt clears the file input
What is a common assertion after uploading a file using sendKeys?
ACheck that the file input is empty
BCheck that the file name is displayed on the page
CCheck that the browser URL changed
DCheck that the page title changed
Which of the following is NOT a valid locator strategy for file input elements?
ABy.osDialog
BBy.name
CBy.xpath
DBy.id
Explain how to upload a file in Selenium using sendKeys and why this method works.
Think about how Selenium interacts with the browser elements.
You got /4 concepts.
    Describe best practices for verifying a file upload in an automated test.
    Focus on what you can see on the web page after upload.
    You got /4 concepts.