Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webdriver: File Upload

Tags:

c#

webdriver

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.

like image 258
Reflux Avatar asked Jul 21 '10 14:07

Reflux


2 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')
like image 111
Ben Butler-Cole Avatar answered Sep 29 '22 12:09

Ben Butler-Cole


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.

like image 43
Sergii Pozharov Avatar answered Sep 29 '22 13:09

Sergii Pozharov