File Upload
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.
16
20
Show full example
package dev.selenium.elements;
import dev.selenium.BaseChromeTest;
import java.io.File;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class FileUploadTest extends BaseChromeTest {
@Test
public void fileUploadTest() {
driver.get("https://the-internet.herokuapp.com/upload");
File uploadFile = new File("src/test/resources/selenium-snapshot.png");
WebElement fileInput = driver.findElement(By.cssSelector("input[type=file]"));
fileInput.sendKeys(uploadFile.getAbsolutePath());
driver.findElement(By.id("file-submit")).click();
WebElement fileName = driver.findElement(By.id("uploaded-files"));
Assertions.assertEquals("selenium-snapshot.png", fileName.getText());
}
}
11
15
<details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">os</span>
from selenium import webdriver from selenium.webdriver.common.by import By def test_uploads(driver): driver.get("https://the-internet.herokuapp.com/upload") upload_file = os.path.abspath( os.path.join(os.path.dirname(file), "..", "selenium-snapshot.png")) file_input = driver.find_element(By.CSS_SELECTOR, "input[type='file']") file_input.send_keys(upload_file) driver.find_element(By.ID, "file-submit").click() file_name_element = driver.find_element(By.ID, "uploaded-files") file_name = file_name_element.text assert file_name == "selenium-snapshot.png"
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/python/tests/elements/test_file_upload.py#L12-L14" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
20
24
Show full example
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
namespace SeleniumDocs.Elements
{
[TestClass]
public class FileUploadTest : BaseChromeTest
{
[TestMethod]
public void Uploads()
{
driver.Url = "https://the-internet.herokuapp.com/upload";
string baseDirectory = AppContext.BaseDirectory;
string relativePath = "../../../TestSupport/selenium-snapshot.png";
string uploadFile = Path.GetFullPath(Path.Combine(baseDirectory, relativePath));
IWebElement fileInput = driver.FindElement(By.CssSelector("input[type=file]"));
fileInput.SendKeys(uploadFile);
driver.FindElement(By.Id("file-submit")).Click();
IWebElement fileName = driver.FindElement(By.Id("uploaded-files"));
Assert.AreEqual("selenium-snapshot.png", fileName.Text);
}
}
}
11
15
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'File Upload' do
let(:driver) { start_session }
it 'uploads' do
driver.get('https://the-internet.herokuapp.com/upload')
upload_file = File.expand_path('../spec_support/selenium-snapshot.png', __dir__)
file_input = driver.find_element(css: 'input[type=file]')
file_input.send_keys(upload_file)
driver.find_element(id: 'file-submit').click
file_name = driver.find_element(id: 'uploaded-files')
expect(file_name.text).to eq 'selenium-snapshot.png'
end
end
23
26
Show full example
const {Browser, By, until, Builder} = require("selenium-webdriver");
const path = require("path");
const assert = require('node:assert');
describe('File Upload Test', function() {
let driver;
before(async function() {
driver = new Builder()
.forBrowser(Browser.CHROME)
.build();
});
after(async() => await driver.quit());
it('Should be able to upload a file successfully', async function() {
const image = path.resolve('./test/resources/selenium-snapshot.png')
await driver.manage().setTimeouts({implicit: 5000});
// Navigate to URL
await driver.get('https://the-internet.herokuapp.com/upload');
// Upload snapshot
await driver.findElement(By.id("file-upload")).sendKeys(image);
await driver.findElement(By.id("file-submit")).submit();
const revealed = await driver.findElement(By.id('uploaded-files'))
await driver.wait(until.elementIsVisible(revealed), 2000);
const data = await driver.findElement(By.css('h3'));
assert.equal(await data.getText(), `File Uploaded!`);
});
});