的webdriver:文件上传 [英] Webdriver: File Upload
问题描述
有没有办法在一个的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是文件名
下面的例子将输入的值设置为文件 C:\\ TEMP \\ file.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:\temp\file.txt
:
String script = "document.getElementById('fileName').value='" + "C:\\\\temp\\\\file.txt" + "';";
((IJavascriptExecutor)driver).executeScript(script);
其中驱动
是你的webdriver实例
where the driver
is you WebDriver instance
请不,你必须使用4 \\
为类似Windows的路径,因为你需要通过双回斜线给JavaScript,所以你有逃脱既两个额外的斜杠。另一种选择是使用正斜线,即C:/tmp/file.txt
,它也应该工作
Please not that you have to use four \
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. Other option is to use a forward slash, i.e. "C:/tmp/file.txt"
and it should also work
这篇关于的webdriver:文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!