Is there a way to interact with a File Upload box in webdriver? The form field where the path gets put in is read only so I can't write to that.
7 Answers
You can do this without injecting JavaScript. You just need to get hold of the form field and type into it. Something like (using the Ruby API):
driver.find_element(:id, 'upload').send_keys('/foo/bar')

- 2,011
- 1
- 17
- 23
-
2Did you try this? On my app, where the field certainly *looks* like it's read only, it works well. – Ben Butler-Cole Aug 12 '10 at 15:11
-
anyone try this method for multiple file uploads (selecting multiple files to upload)? I couldn't get it to work. – David May 23 '12 at 02:03
-
2C# Version : driver.FindElement(By.Id("FileUpload1")).SendKeys("C:\\foo\\bar"); – stuartmclark Feb 04 '14 at 09:47
-
@stuartmclark Tried the C# version and the UI acted like it succeeded, but no file was uploaded. – Mr. Kraus Feb 28 '17 at 21:01
You can set the value of your input field using JavaScript. Considering that the id of the field is fileName
the following example will set the value of the input to the file C:\temp\file.txt
:
String script = "document.getElementById('fileName').value='" + "C:\\\\temp\\\\file.txt" + "';";
((IJavaScriptExecutor)driver).ExecuteScript(script);
In this example, driver
is your WebDriver instance.
Please note that you have to use four backslashes (\
) for Windows-like paths because you are required to pass double back-slashes to the JavaScript so you have to escape both with two additional slashes. The other option is to use a forward slash (e.g. "C:/tmp/file.txt"
) and that should also work.

- 140
- 2
- 8

- 17,366
- 4
- 29
- 30
-
Can this method be made to work for uploading multiple files? Say if you did manually, you select multiple files in the popup file selector. I notice it shows selected files as a double quoted list of filenames separated by a space. If this method works for that, what's the syntax for the JS code? – David May 23 '12 at 02:02
-
4This actually will not work, due to security reasons. E.g. most of the browsers will block this operation! Actually it worked with send_keys method (without executing javascript) – Oleg Tarasenko Jan 02 '14 at 18:46
-
@OlegTarasenko - I noticed the same thing. See https://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html – Joseph238 Mar 18 '21 at 00:15
-
What can we do when there is no `` element? I am working with a website where there is a custom button that calls some JS to open a file dialog and then the file is instantly uploaded. There is no element for me to call `document.getElementById` on. – Aaron Franke Jan 29 '22 at 21:55
-
@aaron-franke looks like you have to identify the script and call it directly in a similar way – Sergii Pozharov Apr 28 '22 at 16:07
For C#, SendKeys()
works but you have to use \
in your file path instead of /
For example, the follow works :
string filePath = @"drive:\path\filename.filextension";
driver.FindElement(By.Id("fileInput")).SendKeys(filePath);
But the following doesn't work :
string filePath = "drive:/path/filename.filextension";
driver.FindElement(By.Id("fileInput")).SendKeys(filePath);

- 4,653
- 2
- 31
- 40
-
4This is platform dependent on Linux and macOS it's forward slash. ( / ) – frankhommers May 11 '20 at 14:38
The problem I found is the upload dialog hangs the webdriver until closed. That is, the element.click which invokes the upload dialog does not return until that upload dialog is closed. To be clear, upload dialog means an OS-native file selection.
Here is my solution (it's a bit complicated but *shrug* most workarounds for selenium webdriver problems must get complicated).
# presumes webdriver has loaded the web page of interest
element_input = webdriver.find_element_by_css_selector('input[id="uploadfile"]')
handle_dialog(element_input, "foobar.txt")
def handle_dialog(element_initiating_dialog, dialog_text_input):
def _handle_dialog(_element_initiating_dialog):
_element_initiating_dialog.click() # thread hangs here until upload dialog closes
t = threading.Thread(target=_handle_dialog, args=[element_initiating_dialog] )
t.start()
time.sleep(1) # poor thread synchronization, but good enough
upload_dialog = webdriver.switch_to_active_element()
upload_dialog.send_keys(dialog_text_input)
upload_dialog.send_keys(selenium.webdriver.common.keys.Keys.ENTER) # the ENTER key closes the upload dialog, other thread exits
Using python 2.7, webdriver 2.25.0, on Ubuntu 12, with firefox.

- 6,169
- 7
- 37
- 63
-
Giving this a try in the morning. I will never understand why .NET developers have a compulsion to mess with file upload dialogs, but I swear they take up more time than any ten other element types combined. – Skip Huffman Sep 12 '12 at 19:56
-
@JamesThomasMoon I tried this but it is not working, the native OS upload window is popped but it does not receive any inputs nor it does receive Enter in last 3 lines of the function. PLease advise – abhi Jun 26 '13 at 10:18
I am in search of 3rd party libraries too,
Unless there is no any other Window Then this works for me :
in C# add reference for System.Windows.Forms
using System.Windows.Forms;
string url = "http://nervgh.github.io/pages/angular-file-upload/examples/image-preview/";
string path = @"C:\Users\File_Path";
IWebDriver d = new ChromeDriver();
d.Navigate().GoToUrl(url);
d.FindElement(By.XPath("//input[@type='file']")).Click();
hread.Sleep(5000);
System.Windows.Forms.SendKeys.SendWait(path);
System.Windows.Forms.SendKeys.SendWait(@"{Enter}");

- 2,555
- 3
- 21
- 29
-
1Better use the driver.SendKeys and remove the Windows Forms dependency. – frankhommers May 11 '20 at 14:37
in my case i can upload file like solution that write hear but dialog window stuck the process, the driver cant reference to close this window, so i kill him manually:
foreach (var p in Process.GetProcessesByName("chrome"))
if (p.MainWindowTitle.ToLower().Contains("open"))
p.Kill();

- 2,387
- 2
- 16
- 17
We can use following (ruby API)
@driver.find_element(:xpath, "html/body/div[1]/div[2]/div[1]/form/div[4]/div[7]/table/tbody/tr[1]/td[2]/input").send_keys "C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"
This is helped me to upload image.

- 515
- 5
- 13