Tune in for the Selenium Community Live scheduled for July 30th, 2025.
Join us!
File Upload
Como subir arquivos com Selenium
Because Selenium cannot interact with the file upload dialog, it provides a way
to upload files without opening the dialog. If the element is an input element with type file,
you can use the send keys method to send the full path to the file that will be uploaded.
packagedev.selenium.elements;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;importjava.time.Duration;importjava.io.File;importjava.nio.file.Path;importjava.nio.file.Paths;importjava.time.Duration;publicclassFileUploadTest{@TestpublicvoidfileUploadTest(){WebDriverdriver=newChromeDriver();driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1000));driver.get("https://the-internet.herokuapp.com/upload");Pathpath=Paths.get("src/test/resources/selenium-snapshot.png");FileimagePath=newFile(path.toUri());//we want to import selenium-snapshot file.driver.findElement(By.id("file-upload")).sendKeys(imagePath.toString());driver.findElement(By.id("file-submit")).submit();if(driver.getPageSource().contains("File Uploaded!")){System.out.println("file uploaded");}else{System.out.println("file not uploaded");}driver.quit();}}
const{suite}=require('selenium-webdriver/testing');const{Browser,By}=require("selenium-webdriver");constpath=require("path");suite(function(env){describe('File Upload Test',function(){letdriver;before(asyncfunction(){driver=awaitenv.builder().build();});after(()=>driver.quit());it('Should be able to upload a file successfully',asyncfunction(){constimage=path.resolve('./test/resources/selenium-snapshot.png')awaitdriver.manage().setTimeouts({implicit:5000});// Navigate to URL
awaitdriver.get('https://www.selenium.dev/selenium/web/upload.html');// Upload snapshot
awaitdriver.findElement(By.id("upload")).sendKeys(image);awaitdriver.findElement(By.id("go")).submit();});});},{browsers:[Browser.CHROME,Browser.FIREFOX]});