File Upload using Selenium

839 views
Skip to first unread message

Manoj Pahuja

unread,
Dec 7, 2011, 7:08:16 PM12/7/11
to Selenium Users
Hi All

I know this has been talked about several times at different places
but I am not able to find an appropriate solution for this problem. I
am hoping somebody on this forum can help me with this.

I have a need to upload a file using Selenium web driver preferably
but if needed can use Selenium RC. The two options availble right now
are
- Use AutoIt tool to create a script to upload a particular file. The
AutoIt script can be created using the following:

WinWaitActive("Choose file")
Send("C:\attach\samplefile.txt") \\location of the file you want to
attach to the form and submit
Send("{ENTER}")

then use the following code to call this.

package com.company;


import java.io.IOException;
import com.thoughtworks.selenium.Selenium;

public class AddAttachment {
public static void attach(Selenium selenium, String fileName) {
try {
String[] commands = new String[]{};
commands = new String[]{"c:\\test\\attachFile.exe"}; //
location of the autoit executable
Runtime.getRuntime().exec(commands);
} catch (IOException e) {}

//autoit executable is now waiting for a "Choose file" dialog
to popup
selenium.click("name=browseButton");
//once the "Choose file" dialog is opened, the autoit will
input the path and file name
}
}

but this option restrict me to upload only one file at a time which is
not what i want to do. I got to be able to upload different files
using a user configurable element


- Using user-extensions.js in the selenium RC. I have not been able to
find appropriate information on this in order to be able to try this.
Can somebody help me with this with guidance on setting up the project
and with getting the appropriate user extention for this?

Any other suggesstions will be greatly appreciated.

Thanks
Manoj

Simon Stewart

unread,
Dec 8, 2011, 9:57:48 AM12/8/11
to seleniu...@googlegroups.com
In WebDriver, just use "WebElement.sendKeys" using the file name as the argument to the file input element.

Regards,

Simon

Manoj

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Manoj Pahuja

unread,
Dec 8, 2011, 6:31:20 PM12/8/11
to Selenium Users
Thanks Simon, but this not able to take the file path of the file that
I am trying to upload. Can you please elaborate how can I use sendkeys
to mention the specific file at a location .

On Dec 8, 6:57 am, Simon Stewart <simon.m.stew...@gmail.com> wrote:
> In WebDriver, just use "WebElement.sendKeys" using the file name as the
> argument to the file input element.
>
> Regards,
>
> Simon
>

> >http://groups.google.com/group/selenium-users?hl=en.- Hide quoted text -
>
> - Show quoted text -

Rivlin Pereira

unread,
Dec 9, 2011, 10:44:52 AM12/9/11
to seleniu...@googlegroups.com
WebElement element;

element = driver.findElement(By.xpath("//input[(@id='edit-upload')]")).sendKeys("enter path here");   

Amey

unread,
Dec 9, 2011, 11:13:46 AM12/9/11
to Selenium Users
Use this Autoit script

==============


AutoItSetOption("WinTitleMatchMode","2") ; set the select mode to
select using substring

;Normally run from command line
if($cmdLine[0] > 2) then
$titlex = $cmdLine[1] ;Title of the window
$form = $cmdLine[2] ;Name of the file upload/save form object
$file = $cmdLine[3] ;Path of the file to upload
Else
;Testing fields
$titlex = "Files"
$form = "ctl00$ContentPlaceHolder1$FilesView$ctl02$NewFile"
$file = "C:\\Users\filename.txt"
EndIf

WinWait($titlex) ; match the window with substring
$title = WinGetTitle($titlex) ; retrives whole window title
WinSetState($title, "", @SW_MAXIMIZE) ;Maximize the window incase
button is hidden
WinActivate($title)
WinWaitActive($title)

$oIE = _IEAttach ("Files")
$oT = _IEGetObjByName ($oIE, $form)
;Move the mouse to the button on the form and click it
MouseMove (_IEPropertyGet ($oT, "screenx") + _IEPropertyGet ($oT,
"width") - 10, _IEPropertyGet ($oT, "screeny") + _IEPropertyGet ($oT,
"height") / 2)
MouseClick ("left")

;Wait for upload screen then input the file and close it
WinWait ("Choose File to Upload")
$hChoose = WinGetHandle ("Choose File to Upload")
ControlSetText ($hChoose, "", "Edit1", $file)
ControlClick ($hChoose, "", "Button2")

;Restore window state
WinSetState($title, "", @SW_RESTORE)

==============

And then when calling the autoit exe file, you could pass the file
name as a parameter.
If no file name is passed, a default file will be used which is
defined in the autoit script as ""C:\\Users\filename.txt""


Also some info on the upload a resume.
As far as I know, the sendkeys function will work in browser mode
"*chrome" or basically only on firefox.

When anyother browser is used to test IE or chrome, the sendkeys
function will not work.

let me know if this was helpful.

Manoj Pahuja

unread,
Dec 9, 2011, 5:20:46 PM12/9/11
to Selenium Users
Thanks all for the help, here is what worked for me:
I used the following AutoIt script:
;Title of the window used for uploading the fileWinWaitActive("File
Upload")  ;File to be uploadedSend("C:
\samplefile.txt") Send("{ENTER}")
Save this file as AddFile.au3.
And then in my test I use the following code to call the exe file
created by compiling the above AutoIt script

try {            String[] commands = new String[]{};          
 commands = new String[]{"C:\\Program Files (x86)\\AutoIt3\\Examples\
\Addfile.exe"}; //location of the autoit executable          
 Runtime.getRuntime().exec(commands);   } catch (IOException e) {}
  WebElement addFileElement = driver.findElement(By.className("add-
file")); // you can find the element to upload the file in different
way, i used class name here, you can use xpath
addFileElement.click();
We need to support multiple browsers for our product so the script
below would have not allowed to use it across different browsers.
Also "sendkeys" with the path did not work for me, selenium was not
able to detect the file upload window. You can probably use this
option if your file upload has a text box which can take the file path
directly.
Once again thanks all for all the help.
Manoj

Phani.B

unread,
Jul 4, 2012, 6:36:46 AM7/4/12
to seleniu...@googlegroups.com
Something called autoitx4java is very useful if you are using Selenium+java. its simple and easy to use.

Somesh Bansal

unread,
Jul 5, 2012, 2:21:40 AM7/5/12
to seleniu...@googlegroups.com
Hi Guys,

I am too facing with same kind of problem, but the difference is that 'Browse & Upload button' is embedded in Flash window and selenium IDE is not recording the same. It requires any video file to be uploaded

I am using
driverInstance.findElement(By.name("kaltura_player")).sendKeys("C:\\Documents and Settings\\somesh.bansal\\My Documents\\UploadVideo.mp4");

Still no output... Please refer "https://groups.google.com/forum/#!msg/selenium-users/chGPm7G3kv0/dr_VTJcKp5cJ" link for detailed query

Somesh

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/TO26QPgPgOAJ.

To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

Reply all
Reply to author
Forward
0 new messages