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.
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')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With