0

I am new to the C# world and I want to know how can a Windows file upload form be automated using Selenium in a .Net core project as it doesn't support AutoIt.

user4157124
  • 2,809
  • 13
  • 27
  • 42
123
  • 13
  • 6
  • First option is in some of the website you can pass the path of the file to be uploaded in the text box using the .sendKeys() method. Second option is that you can pass the path of the file to be uploaded by setting the value (or similar) attribute using the JavaScriptExecutor Class. Third option is to use the Robot class in Java. Also go through this [link](https://stackoverflow.com/questions/16896685/how-to-upload-file-using-selenium-webdriver-in-java) – Alok May 02 '20 at 11:24
  • Sendkeys doesn't work in my scenario as I have to click on the button first and then the Window pop up appears. Can you give me an example of how this can be done using the JavaScriptExecutor Class. – 123 May 02 '20 at 23:11
  • Share the snippet of the HTML code of the page after you manually select the file to upload. – Alok May 03 '20 at 02:50
  • Hi Alok, I can't share the actual code because of my company's security policy but the functionality is very similar to Gmail's attach feature. So while creating a new mail if you click on the attach button it takes you to the file upload window. I am looking for a way to automate this very behavior. – 123 May 03 '20 at 17:14
  • Even I could not find an example on net to show you, Sorry. Will keep searching. Like @Dazed suggested you need to note what HTML code changes before and after you select a file to upload. You will find your answer there. For the Time being I think Robot Class of Java can also be used to fix the issue. – Alok May 03 '20 at 18:14

1 Answers1

2

Zehra - It really depends on how precise you want to get to mimicking the user functionality. Typically you click a button that opens a windows dialog where you locate the file on your pc/phone. Since this is really not testing the application and is just a Windows function, I just use send keys. If you want to get more precise, you can look at AutoIT but I would suggest just doing send keys.

Set your location for the file.

 string filePath = @"C:\MyFiles\Test.jpg";

Then find the path to the input for the file upload.

 driver.FindElement(By.XPath("//div[@class='FileUploadInput']")).SendKeys(filePath);

If you have a spinner or a bar for the upload process, I would wait until that element is no longer visible and then proceed.

As an example - go here - https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_get

In the example if you look at the "choose file" element, it looks like:

<input type="file" id="myFile">

You would then just do:

string filePath = @"C:\MyFiles\Test.jpg";
driver.FindElement(By.Id("myFile")).SendKeys(filePath);
Dazed
  • 1,527
  • 1
  • 13
  • 25
  • Hi Dazed, SendKeys doesn't work in my scenario as I do not have a textbox where I can pass the file path. I have to first click on the button and then the Window pop up appears where I have to send the name of my file. AUTOIT is not supported by .Net Core. – 123 May 02 '20 at 23:01
  • You are not sending to the text box per se. You need to find the input element for the file upload. The upload is all done behind the scenes. Can you post the html markup for the upload area? – Dazed May 03 '20 at 00:04
  • Hi Dazed, I can't share the actual code because of my company's security policy but the functionality is very similar to Gmail's attach feature. So while creating a new mail if you click on the attach button it takes you to the file upload window. I am looking for a way to automate this very behavior. – 123 May 03 '20 at 17:11
  • They are all pretty much the same. If you look at the gmail upload area you will see: This is the input for the file so you could use xpath= //input[@name='Filedata'] and use sendkeys to send the path as I showed you in my answer. This will upload the file for you. Make sense? so for gmail you would use string filePath = @"C:\MyFiles\Test.jpg"; driver.FindElement(By.Xpath("//input[@name='Filedata'] ")).SendKeys(filePath); – Dazed May 03 '20 at 17:23
  • Hi @Dazed why have you used '@' in string filePath = @"C:\MyFiles\Test.jpg"; – Alok May 04 '20 at 06:49
  • Hey @Alok - The @ symbol turns off \ escaping. – Dazed May 04 '20 at 12:13