file upload in selenium webdriver using firefox.....

9,270 views
Skip to first unread message

webdriver user

unread,
Aug 23, 2012, 3:25:52 AM8/23/12
to seleniu...@googlegroups.com
Hello all,
        

   I'm trying to upload a file using Selenium webdriver + Java. However, when the file modal window opens, the cursor does NOT click on the input box for the filename. I have tried all the options given by the selenium members in the forum like the ones below:

1) /*setClipboardData("C:\\Users\\Demo\\Desktop\\today.txt");
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);


2) Robot r = new Robot();
r.keyPress(KeyEvent.VK_C);        // C
r.keyRelease(KeyEvent.VK_C);
r.keyPress(KeyEvent.VK_COLON);    // : (colon)
r.keyRelease(KeyEvent.VK_COLON);
r.keyPress(KeyEvent.VK_SLASH);    // / (slash)
r.keyRelease(KeyEvent.VK_SLASH);
// etc. for the whole file path------------------------------------------------------------------------->>>>>how to give the file path here? can you elaborate on this ?
//r.keyPress(KeyEvent.)

r.keyPress(KeyEvent.VK_ENTER);    // confirm by pressing Enter in the end
r.keyRelease(KeyEvent.VK_ENTER) ;

3) ALso tried:
WebElement fileInput = driver.findElement(By.xpath("//input[@type='file']"));
fileInput.sendKeys("C:/Users/Demo/Desktop/today.txt");

In this case it opens up the modal window , but the cursor is NOT blinking. 

Can you plz help me get thru this? Am using firefox browser and selenium webdriver 


Thanks !!!

Krishnan Mahadevan

unread,
Aug 23, 2012, 3:30:41 AM8/23/12
to seleniu...@googlegroups.com
Rashmi,
Selenium/WebDriver wont be able to interact with the File Dialog box that is shown by the OS. 

You would need to just type in the path of the file into the type="file" and then resume by clicking on the submit button[or whatever button that is available]

Option[3] that you tried would be the easiest/straight forward way of dealing with it.

Perhaps you can show us the corresponding html snippet as well ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"




--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/qHuojW7upJIJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Rashmi Rohit

unread,
Aug 23, 2012, 1:01:40 PM8/23/12
to seleniu...@googlegroups.com, Krishnan Mahadevan
I don't think the html snippet will help in this matter as it is a regular windows modal window which opens up when we want to upload any document...


My code does open up the modal window, but doesn't seem to be blinking on the input window, so I can't seem to put in the file path there...

Do you have any code snippet which works for this kind of file upload??? 

Here is the HTML snippet: Am trying  to click on the Add Files button > model window opens, I want to click on the input box and type in the file path and click enter.






div id="uploader_container" class="plupload_container" title="Using runtime: html4">
<div class="plupload">
<div class="plupload_header">
<div class="plupload_content">
<div class="plupload_filelist_header">
<ul id="uploader_filelist" class="plupload_filelist"></ul>
<div class="plupload_filelist_footer">
<div class="plupload_file_name">
<div class="plupload_buttons">
<a id="uploader_browse" class="plupload_button plupload_add" href="#" style="position: relative; z-index: 0;">Add files</a>
<a class="plupload_button plupload_kickstart plupload_disabled" href="#">Start upload</a>
</div>
<span class="plupload_upload_status"></span>
</div>
<div class="plupload_file_action"></div>
<div class="plupload_file_status">
<div class="plupload_file_size">
<div class="plupload_progress">
<div class="plupload_clearer">&nbsp;</div>
</div>
</div>
</div>
</div>
<input id="uploader_count" type="hidden" value="0" name="uploader_count">
</div>


Thanks 

Mike Riley

unread,
Aug 23, 2012, 4:57:16 PM8/23/12
to seleniu...@googlegroups.com, Krishnan Mahadevan

Krishnan Mahadevan

unread,
Aug 23, 2012, 10:09:13 PM8/23/12
to Mike Riley, seleniu...@googlegroups.com
Mike,
Mark Collins' article is about "download" but I think Rashmi is looking to deal with "upload". 

Rashmi,
The html snippet you shared didn't have a input type=file at all in it. I did see some css classes being bound to you button. Am not an expert on these stuff so you may perhaps have to share the entire html itself. 


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

DongXiang

unread,
Aug 24, 2012, 5:22:09 AM8/24/12
to seleniu...@googlegroups.com

 
1) /*setClipboardData("C:\\Users\\Demo\\Desktop\\today.txt");
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();
  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
 
Add those two lines (red line) and have a try, I have a similar problem before, without those two lines, it works in IE/Chrome but failed with Firefox, after it, it works for those three browsers.
 

Date: Fri, 24 Aug 2012 07:39:13 +0530
Subject: Re: [selenium-users] file upload in selenium webdriver using firefox.....
From: krishnan.ma...@gmail.com
To: lvsk...@cox.net
CC: seleniu...@googlegroups.com

Mike,
Mark Collins' article is about "download" but I think Rashmi is looking to deal with "upload". 

Rashmi,
The html snippet you shared didn't have a input type=file at all in it. I did see some css classes being bound to you button. Am not an expert on these stuff so you may perhaps have to share the entire html itself. 

On Friday, August 24, 2012, Mike Riley wrote:
Try reading what Mark wrote about this here:

Mark Collin

unread,
Aug 24, 2012, 7:17:18 AM8/24/12
to seleniu...@googlegroups.com

Can you show us your actual code, fileInput.sendKeys("C:/Users/Demo/Desktop/today.txt"); should not be opening up a dialogue, it should just silently insert the file location and then work when you click on the submit button.

--

manoj hans

unread,
Aug 24, 2012, 7:43:44 AM8/24/12
to seleniu...@googlegroups.com
Mike is right rashmi...it will just insert path.

Peter Gale

unread,
Aug 24, 2012, 7:46:03 AM8/24/12
to Selenium Users
> Mike is right ...

On no, not this again?

Did you mean Mark?


Date: Fri, 24 Aug 2012 04:43:44 -0700
From: hman...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] file upload in selenium webdriver using firefox.....
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/vQC7q3DsQT4J.

Rashmi Rohit

unread,
Aug 24, 2012, 1:34:02 PM8/24/12
to seleniu...@googlegroups.com
I just need to insert path of the file  into the input box and then again with the ROBOT help click on the action which enters the path. 

This is only for UPLOADing  the file and not download.

Will try all options and see if it works...

Thanks

Mike Riley

unread,
Aug 24, 2012, 10:51:11 PM8/24/12
to seleniu...@googlegroups.com, Mike Riley
I plead tired eyes.  Sorry for that misread.

Mike

manoj hans

unread,
Aug 25, 2012, 4:56:13 AM8/25/12
to seleniu...@googlegroups.com, Mike Riley
Ya peter i mean Mark..:P
I really confused about these two...!!!...in my last post as well i mentioned mike instead of mark...

Debasish Dutta

unread,
Jan 8, 2013, 4:39:37 AM1/8/13
to seleniu...@googlegroups.com
Hi,

Check below script.May it will work.

driver.findelemet(By.xpath("xpathof the field where u will give path")).sendkeys("path of the file);



Thanks
Debasish

On Tue, Jan 8, 2013 at 12:09 PM, ayushi rajpurohit <rajpuroh...@gmail.com> wrote:
Hi,

I encountered the same problem,

i wrote the following command

WebElement fileInput = driver.findElement(By.id("product_imageUploader"));
    fileInput.click();
    fileInput.sendKeys("C:/bills_cmpy.jpg");

But it is not taking the path

If you have found the solution then please let me also know

Thanks in Advance

Cheers !!
--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/JhXURB3l9qkJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

Thanks & Regards,

Debasish Dutta

Mark Collin

unread,
Jan 8, 2013, 5:10:39 AM1/8/13
to seleniu...@googlegroups.com
Don't click on the fileInput before doing a sendKeys


On 08/01/2013 06:39, ayushi rajpurohit wrote:
Hi,

I encountered the same problem,

i wrote the following command

WebElement fileInput = driver.findElement(By.id("product_imageUploader"));
    fileInput.click();
    fileInput.sendKeys("C:/bills_cmpy.jpg");

But it is not taking the path

If you have found the solution then please let me also know

Thanks in Advance

Cheers !!

On Thursday, 23 August 2012 12:55:52 UTC+5:30, webdriver user wrote:

Arran

unread,
Jun 11, 2013, 5:41:23 AM6/11/13
to seleniu...@googlegroups.com
I don't think you understand the difficulty behind incorporating that kind of functionality. In most cases, sending the file path will do the trick. However a lot of people actively click the Browse button. Selenium can only do one thing and throw it's hands up in the air at this point. People see this, think Selenium is at fault, they think it's crap and never use it again - and if people actually listened, actually researched the problem and understood the decisions behind the workarounds and why it hasn't been implemented, we wouldn't have this problem every week on this forum and StackOverflow.

On Monday, 10 June 2013 15:39:42 UTC+1, sami wrote:
sendkeys will not work for css based file dialog popup. That is crap.. try autoit kit with java.

Gadigeppa Jattennavar

unread,
Jul 9, 2014, 10:16:58 AM7/9/14
to seleniu...@googlegroups.com

When you click on ‘Choose File’ button, OS native file select dialog will be opened. This dialog will not be recognized by your driver, so when uploading any file you should never click on ‘Choose File’ button. Instead, use sendkeys to select the file,

WebElement El = driver.findElement(By.id("'fileUploadField'"));
El.sendKeys("c:\\temp\\test.txt");

This will select your file without having to select it through file select dialog. This works flawlessly across all supported drivers.

If you are running your test on grid then you should let your remote driver know that the file is residing on local machine and not remote machine. This can be achieved by setting LocalFileDetector for RemoteWebElement,

WebElement El = driver.findElement(By.id("'fileUploadField'"));
((RemoteWebElement) El ).setFileDetector(new LocalFileDetector());
El.sendKeys("c:\\temp\\test.txt");


Here you are setting local file detector for file upload element, so whenever you use sendkeys on this element, your RemoteWebDriver uses this file detector object to detect the file. If the file is detected then it will be base64 encoded and uploaded to remote server through JSON Wire Protocol and the remote fixed path will be selected as a file path.

Please check my blog http://muthutechno.wordpress.com/2014/07/09/uploading-files-in-selenium-webdriver/


On Thursday, August 23, 2012 12:55:52 PM UTC+5:30, webdriver user wrote:

Serguei Kouzmine

unread,
Aug 24, 2014, 9:12:50 PM8/24/14
to seleniu...@googlegroups.com
9:11 PM (less than a minute ago)
Hello
I developed a code in C# to deal with download dialogs on Windows: set the local file name  / path. save the file. Please look at  the article, code is provided too,
http://www.codeproject.com/script/Articles/ArticleVersion.aspx?waid=138391&aid=799161#54C724BE37D04CE0AD9584606ACF5560


Thanks,

Serguei Kouzmine

141.png
144.png

Miller Chiang

unread,
Mar 7, 2015, 11:23:24 PM3/7/15
to seleniu...@googlegroups.com
Hi David or all, I have the same issue that it works from Chrome but not Firefox.   And I added your two lines, but it still does not work.  However, if I exit out the dialog window and re-click browse button manually from where it hangs, then this time it will paste the file path and click enter automatically.   Does anyone know why it happens like this?   My code is very similar to what has been mentioned here.   I don't mind copy here again. 

I have Selenium 2.44 & FF v31.  JDK 1.7

// click import button 
ImportPage.getImportBtn().click();

// click browse button
ImportPage.getBrowseBtnImport().click();
// set the file name in clipboard
StringSelection ss = new StringSelection(filePath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,null);
Thread.sleep(3000);//Sleep time to detect the window dialog box
// Perform native keystrokes for CTRL+V and ENTER keys
try {
Robot robot = new Robot();
robot.delay(1500);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
    
        robot.delay(1000);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.delay(1500);
   
} catch (Exception exp){
exp.printStackTrace();
}


CH!NN@ K

unread,
Mar 8, 2015, 4:10:27 AM3/8/15
to seleniu...@googlegroups.com
File Upload using selenium Webdriver in Java : http://techlearn.in/content/file-upload-using-selenium-webdriver-java

---
Thanks & Regards,
Purushotham Karimbedu,
Drupal Developer and QA Engineer,
Website for Selenium : http://techlearn.in


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages