How to upload file ( picture ) with selenium, python


How to upload a picture on a web application with the selenium testing tool? I am using python.

I tried many things, but nothing worked.

Asked By: Filip



Upload input control opens a native dialog (it is done by browser) so clicking on the control or browse button via Selenium will just pop the dialog and the test will hang.

The workaround is to set the value of the upload input via JavaScript (in Java it is done via JavascriptExecutor) and then submit the form.

See this question for sample in C#, I am sure there’s also a way to call JavaScript in Python but I never used Selenium Python bindings

Answered By: Sergii Pozharov

What I’m doing is this (make sure drv is an instance of webdriver):


and then find your submit button and click it.

Answered By: karloskar

All these approach wont work with modern image uploaders in olx !!!
Alternative approach (only for windows )

1. Automation of windows can be done using Autoit 
2. Install Autoit and SciTe Script editor 
3. Autoit code :(imageupload.au3)

WinActivate("File Upload");  for chrome use "open" that is the name of the window that pops 
send("D:imagesimage1.png");  path of the file

4. compile it to get an .exe file(imageupload.exe)
5. Now from python call the .exe file like

import os
import os.system('C:imagesimageupload.exe') #path of the .exe file
Answered By: saravanan

I have used below script format to upload the images. This may help you.

   driver.find_element_by_id("Id of the element").clear()            
   driver.find_element_by_id("Id of the element").send_keys(Imagepath)

if you do not have ID of the object ,then you can use xpath or css selector accordingly.

Answered By: Tester P
import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")   

Will resolve the issue

Answered By: user1859723

I am using fine-uploader, running selenium tests with pytest and this worked for me:

elm = driver.find_element_by_xpath("//input[@type='file']")
elm.send_keys(os.getcwd() + "/tests/sample_files/Figure1.tif")

No form submission or Enter key is needed in my case.

Answered By: AnaPana

Using splinter :


Answered By: Anjan Dash

I added an answer for anyone looking to use deal with the annoying msofiledialogs. This is working off of saravanan’s proposed solution, but more fleshed out for Python.

I had a similar problem with a script I’m working on for a company on the side. I’m attempting to upload documents for a company’s clients, but due to the way their site worked, I could not utilize send_keys to directly send the path, so I had to rely on msofiledialog.

  1. You only need to install AutoIt or just “pip install -U pyautoit” through the cmd screen

  2. type “import autoit” on your script page

  3. Type the following before the file dialog pops up in your script:


It will look for the open file dialog window and fill it out and press enter.
“Open” is the title of my file dialog screen. Put the title of yours in place of “Open”. There are more creative ways to utilize AutoIt’s functions, but this is an easy, straightforward way for beginners.

Edit: DO NOT. DO NOT use control_send on most things if you can avoid it. It has a well-known issue of sending erroneous text. In my case, the colon in my file path was being turned into a semi colon. If you need to send input keys, it should be fine, however if you need to send text, use control_set_text. It has the same syntax.

Answered By: Noctsol

Here is the code that i used:

Imagepath = "C:UserDesktopimage.png"

I accept the Answer by karloskar. Note It is not working for FireFox (59). And it is works with Chrome Driver only.

Answered By: Arun K

full code to achieve file upload using autoit tool.
u can just copy paste this and u can run, it will work since it is a acti-time demo.

from selenium import webdriver
from import WebDriverWait
from import expected_conditions as ec
from import By
from selenium.webdriver.common.keys import Keys
import time
import os

def fileUploading():

    driver = webdriver.Firefox()
    wait = WebDriverWait(driver, 10)
    driver.find_element(By.NAME, "pwd").send_keys("manager")
    driver.find_element(By.XPATH, "//div[.='Login ']").click()
    wait.until(ec.element_to_be_clickable((By.XPATH, "(//div[@class='popup_menu_icon'])[3]")))
    driver.find_element(By.XPATH, "(//div[@class='popup_menu_icon'])[3]").click()
    wait.until(ec.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Contact actiTIME Support')]")))
    driver.find_element(By.XPATH, "//a[contains(text(),'Contact actiTIME Support')]").click()
    wait.until(ec.element_to_be_clickable((By.XPATH,"//div[@class='dz-default dz-message']")))
    driver.find_element(By.XPATH,"//div[@class='dz-default dz-message']").click()


below is the content of autoit code:

WinWaitActive("File Upload")

download autoIt and autoIt SCITE editor tool.
once done install autoit and the open the scite editor and paste the above code and save it with .au3 extension and once saved, right click on the file and select complile script(x64), now .exe file is created.

now use the below code:

Answered By: Mallikarjuna B

A very easy way to control components like windows file selector (or just your OS in general) is by using pyautogui. You can install pyautogui through pip

import pyautogui
... # set the webdriver etc.
element_present = EC.presence_of_element_located((By.XPATH, "//button[@title='Open file selector']"))  # Example xpath

WebDriverWait(self.driver, 10).until(element_present).click() # This opens the windows file selector

Answered By: XRaycat
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
    driver = webdriver.Firefox()
    def upload_file():
        upload_file = driver.find_element_by_id("paste here id of file which 
        enter code here`shown in html code...")

       upload_file.send_keys("copy the path of file from your pc with name and 
       paste here...")

if toast message is disappeared within seconds you can use Chropath extension in chrome to find its xpath

This is a pure python code.

Answered By: GHULAM NABI

Instead of using control_send, use control_set_text to resolve inconsistencies present in the string sent to the window. See this link for reference:

import autoit

autoit.control_set_text("Open", "Edit1", imgPath)
Answered By: sherlock

Use PyAutoGui if sendkeys function is not working on buttons.

sample code:

import pyautogui
driver.find_element_by_xpath("Enter Xpath of File upload button").click()
time.sleep(4) #waiting for window popup to open
pyautogui.write(r"C:UsersAmarKumarFilesForUploadingimage.jpg") #path of File'enter')
Answered By: Amar Kumar

You can easily add this one line of code to solve the problem:

driver.find_element_by_xpath("your fullpath").send_keys("C://1.png(your file root")

But pay attention, sometimes maybe you put a wrong xpath in first field. follow below steps for reach to rightful xpath:

  1. open the inspect and click exactly on the box which you want to
    upload the file.
  2. right click on the html code and select xpath full address from copy
    sub menu.
  3. paste the root in xpath field in the code.

enter image description here

Answered By: Morteza Oskuei

If you are using service you will get an exception

service = Service('driver_path')
driver = webdriver.Remote(service.service_url)
choose_image = driver.find_element(By.ID, 'id')

Exception :

selenium.common.exceptions.WebDriverException: Message: unknown command: unknown command: session/$sessionId/se/file

Working code (suggestion – use id of the element instead of other)

choose_image=driver.find_element(By.ID, 'id')
choose_image.send_keys(os.path.join(os.getcwd(), 'image.jpg'))
Answered By: Hariom Deo

In case you want to upload multiple pictures, you can alter the single image answer as follows:

images = ["/path/to/image1.png", "/path/to/image2.png","/path/to/image3.png"]

Note that the input form should have the multiple attribute set to True.

Answered By: Saaru Lindestøkke

I post a solution that i consider best. it is a modification on solution by @sherlock (see my comment on that post)

import autoit
autoit.control_set_text("Open", "Edit1", imgPath)
autoit.control_click("Open", "Button1")
Answered By: fekiri malek

Install these three

sudo apt-get install python3-tk python3-dev

pip install tk

pip install PyAutoGUI

use these two line of code.


Answered By: Achal Singh