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.
-
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 linkAlok– Alok2020-05-02 11:24:50 +00:00Commented May 2, 2020 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– 1232020-05-02 23:11:54 +00:00Commented May 2, 2020 at 23:11
-
Share the snippet of the HTML code of the page after you manually select the file to upload.Alok– Alok2020-05-03 02:50:07 +00:00Commented May 3, 2020 at 2: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– 1232020-05-03 17:14:17 +00:00Commented May 3, 2020 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– Alok2020-05-03 18:14:56 +00:00Commented May 3, 2020 at 18:14
1 Answer
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);