问题
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