0

I'm trying to automate a scenario in which I have to upload document but I couldn't handle the File Upload window once I click the 'Choose File' button on my web application.

I'm using C# language and Chrome browser.

Cyril Durand
  • 15,834
  • 5
  • 54
  • 62
ravi 0401
  • 1
  • 1
  • 1
  • 1
  • What did you try ? Do you have any error message ? Please read [how to ask](http://stackoverflow.com/help/how-to-ask) and edit your question to improve your chance to receive answer :) – Cyril Durand Mar 31 '15 at 22:45
  • 1
    Did you tried using SendKeys(path to file)? instead of clicking on Choose File try sending filepath using above said method. – Vivek Singh Apr 01 '15 at 09:28

4 Answers4

0

My test case in C#:

[Test, Description("JAVA_67350: Filename is shown as tooltip in browse button after upload is cancelled in Firefox browser ")]
[Component(Component.UploadBox)]
[Priority(Priority.High)]
[CustomerID(160100)]

public void JAVA_67350()
{
    driver.Navigate().GoToUrl("http://localhost/JavaScript/CR_Samples/JavaScriptSamples/JAVA_67350/UploadBoxUntitled.html");
    driver.Manage().Window.Maximize();
    Thread.Sleep(3000);
    //clicking upload button
    driver.FindElement(By.XPath(".//*[@id='UploadDefault']/div[1]/input[2]")).Click();
    Thread.Sleep(TimeSpan.FromSeconds(1));
    //file uploading
    SendKeys.SendWait("C:\\UploadBox\\Examplefordocx.docx" + @"{RIGHT}");
    Thread.Sleep(500);
    SendKeys.SendWait(@"{TAB}");
    Thread.Sleep(500);
    SendKeys.SendWait(@"{TAB}");
    Thread.Sleep(500);
    SendKeys.SendWait(@"{ENTER}");
    Thread.Sleep(3000);
    }
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
SivajiRaw
  • 43
  • 4
0

you can use SendKeys

using System.Windows.Forms;

SendKeys.SendWait(@"C:\temp\file.txt");
SendKeys.SendWait(@"{Enter}");
-1

Selenium doesn't have any inbuilt functions to handle file upload/interacting with windows dialogues(print dialogue)/windows popups.

If you want to interact with windows objects, you can use AutoIT

It is a great tool to interact with windows objects and very easy to use. See the below link for steps to work with.

http://www.toolsqa.com/seleniu-webdriver/autoit-selenium-webdriver/

  • Selenium provides the functionality to upload the File in most of the cases. http://stackoverflow.com/questions/16896685/how-to-upload-files-using-selenium-webdriver-in-java. http://seleniumeasy.com/selenium-tutorials/uploading-file-with-selenium-webdriver – Sakshi Singla Apr 07 '15 at 04:29
  • @SakshiSingla Selenium can handle only browser controls not OS controls. If your web application is handling file upload using browser controls, you can use selenium. If not handle those using some third party tools or coding language functions. http://seleniumeasy.com/selenium-tutorials/uploading-file-with-selenium-webdriver – VaraPrasad Pakalapati Apr 07 '15 at 08:42
  • Yes you are right but your answer gives a picture that Selenium cannot handle file upload. Please edit your answer to include the above information. – Sakshi Singla Apr 07 '15 at 09:35
  • @SakshiSingla My answer is specific to asked question not generic. In the question above **File Upload window** is mentioned which is not browser window, it is OS specific window which can not be handled by Selenium. Read the question and my answer again to understand the problem Ravi is facing. – VaraPrasad Pakalapati Apr 07 '15 at 09:55
  • If we click on File upload button on web application, then the behavior would be that Windows dialog will open. However, the same functionality can be handled in Selenium in a different way(without actually opening the windows dialog). In case it cannot be handled with Selenium, then AUtoIt is a good choice – Sakshi Singla Apr 07 '15 at 09:58
-1

You can try the following if your File element has 'input type = file': element(by.css('input[type="file"]')).sendKeys(pathToFile);

This will not open up the File upload windows dialog. Rather this will send the required value to the input File field.

You can refer to the following links for details: http://seleniumeasy.com/selenium-tutorials/uploading-file-with-selenium-webdriver

How to upload file using Selenium WebDriver in Java

Regards,
Sakshi

Community
  • 1
  • 1
Sakshi Singla
  • 2,462
  • 1
  • 18
  • 34