tags:

views:

129

answers:

2

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.

+1  A: 

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);

where the driver is you WebDriver instance

Please not that you have to use four \ 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. Other option is to use a forward slash, i.e. "C:/tmp/file.txt" and it should also work

ZloiAdun
Works perfectly thanks!.
Reflux
A: 

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')
Ben Butler-Cole
The form field is readonly so the above code wont work.
Reflux
Did you try this? On my app, where the field certainly *looks* like it's read only, it works well.
Ben Butler-Cole