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

查看:32
本文介绍了如何使用 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:MyFilesTest.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:MyFilesTest.jpg";
driver.FindElement(By.Id("myFile")).SendKeys(filePath);

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

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