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
2010-07-22 08:40:46
Works perfectly thanks!.
Reflux
2010-07-22 12:50:01
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
2010-07-23 16:39:08
Did you try this? On my app, where the field certainly *looks* like it's read only, it works well.
Ben Butler-Cole
2010-08-12 15:11:08