Webdriver: File Upload(Webdriver:文件上传)
问题描述
有没有办法与 webdriver 中的文件上传框进行交互?放入路径的表单字段是只读的,所以我无法写入.
Is there a way to interact with a File Upload box in webdriver? The form field where the path gets put in is read only so I can't write to that.
推荐答案
您可以使用 JavaScript 设置输入字段的值.考虑到该字段的 id 是 fileName
,下面的示例将输入的值设置为文件 C: empfile.txt
:
You can set the value of your input field using JavaScript. Considering that the id of the field is fileName
the following example will set the value of the input to the file C: empfile.txt
:
String script = "document.getElementById('fileName').value='" + "C:\\temp\\file.txt" + "';";
((IJavaScriptExecutor)driver).ExecuteScript(script);
在本例中,driver
是您的 WebDriver 实例.
In this example, driver
is your WebDriver instance.
请注意,对于类似 Windows 的路径,您必须使用四个反斜杠 (),因为您需要将双反斜杠传递给 JavaScript,因此您必须使用两个额外的斜杠对两者进行转义.另一种选择是使用正斜杠(例如
"C:/tmp/file.txt"
),这也应该有效.
Please note that you have to use four backslashes () for Windows-like paths because you are required to pass double back-slashes to the JavaScript so you have to escape both with two additional slashes. The other option is to use a forward slash (e.g.
"C:/tmp/file.txt"
) and that should also work.
这篇关于Webdriver:文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Webdriver:文件上传


- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01