如何使用Selenium在.Net Core中处理Windows文件上传? [英] How to handle Windows file upload in .Net core using Selenium?

查看:74
本文介绍了如何使用Selenium在.Net Core中处理Windows文件上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#领域的新手,我想知道如何在.Net核心项目中使用Selenium自动实现Windows文件上传表单,因为它不支持AutoIt。

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.

推荐答案

Zehra-实际上取决于您要模仿用户功能的精确程度。通常,您单击一个按钮会打开一个Windows对话框,您可以在其中找到PC /电话上的文件。由于这实际上不是在测试应用程序,而只是Windows的功能,因此我只使用发送键。如果您想获得更高的精确度,可以看看AutoIT,但我建议您只做发送键。

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.

设置文件的位置。

 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.

作为示例-转到此处- https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_get

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">

然后您就可以这样做:

string filePath = @"C:\MyFiles\Test.jpg";
driver.FindElement(By.Id("myFile")).SendKeys(filePath);

这篇关于如何使用Selenium在.Net Core中处理Windows文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆