How to handle Windows file upload in .Net core using Selenium?

不问归期 提交于 2020-05-14 10:25:49

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/61551055/how-to-handle-windows-file-upload-in-net-core-using-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!