How to select image using Selenium Web driver.

Reading Time: 2 minutes

In this blog ,we can select images using Selenium WebDriver. Selenium WebDriver is a web framework that permits us to execute cross-browser tests.Also, this tool is used for automating web-based application testing to verify that it performs expectedly.

Browser Drivers in Selenium

In order to develop a secure connection with the browser, Selenium uses Drivers. Each browser has its own driver that hides the internal logic of the browser’s functionality.

In addition, each automation language has a corresponding browser driver. The following series of actions are performed when a Selenium automation test is triggered:

  • Every Selenium command generates a corresponding HTTP request, which is sent to the browser driver.
  • This request is routed through the HTTP Server.
  • The HTTP Server now drives the instruction execution on the browser.
  • The browser sends back the status to the HTTP Server, which forwards it to the automation script.

Some of the browser drivers are ChromeDriver – For chrome Browser, GeckoDriver – For FireFox Browser, MicrosoftEdge driver – For Edge Browser, etc.

Also, In this blog we will use ChromeDriver.

Select an Image through Selenium Webdriver

For this ,we will use to select the image.This site undoubtedly allows any visitor to upload files without requiring them to sign up.

Uploading files in WebDriver is done by simply using the sendKeys() method on the file-select input field to enter the path to the file to be uploaded.

Here, “No file chosen” is the file selection input box in upload site.

Furthermore ,After inspecting choose file we got the input id that we use in our code.


import org.openqa.selenium.*;


import org.testng.Assert;

import org.testng.annotations.Test;

public class Image {

    WebDriver driver;


    public void setup() {

            System.setProperty("", "drivers/chromedriver");

        driver = new ChromeDriver();



        WebElement add = driver.findElement("file-upload"));


        WebElement logo = driver.findElement("drag-drop-upload"));

        WebElement upload = driver.findElement("file-submit"));;

        WebElement errorMessage = driver.findElement(By.cssSelector("h3"));

        String expectedErrorMessage = "File Uploaded!";

        String actualErrorMessage = errorMessage.getText();

        Assert.assertTrue(actualErrorMessage.contains(expectedErrorMessage), "Actual error message does not contain expected. \nActual: " + actualErrorMessage + "\nExpected: " + expectedErrorMessage);


Furthermore, After running this script, we will be able to upload the image successfully .Here,1.jpeg is selected as a file.

After clicking on Upload we got the message File Uploaded!


Please do check out our other blogs on a similar tech stack,

Written by 

Sakshi Sneha is Software Consultant in Knoldus Inc. She is always eager to learn new and advanced concepts in order to upskill herself.