我可以使用Selenium Webdriver C#在Firefox上传文件,但相同的代码对IE不起作用。我使用的是IE11和最新的IEDriverServer 3.6.0。 我一直在互联网上寻找解决方案,但没有任何作用。我将在此感谢任何帮助
这是我的代码如下。我想知道是否有这样做的javascript方式?
public UploadPage HTUpload(string filePath = "C:\\\\Users\\\\Me\\\\Desktop\\\\Capture.JPG")
{
int retryCount = 0;
while (retryCount < Constants.RETRY_COUNT)
{
try
{
_driver.FindElement(By.Name("files[]")).SendKeys(filePath);
return this;
}
catch (Exception ex) when (ex is WebDriverTimeoutException || ex is TimeoutException)
{
retryCount++;
}
}
return this;
}
<div class="fileupload">
<div class="col-lg-7">
<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
<span>Add files...</span>
<input name="files[]" multiple="multiple" type="file"/>
</span>
<button type="submit" class="btn btn-primary start" name="btnupload">
<span>Start upload</span>
</button>
<button type="reset" class="btn btn-warning cancel" name="btncancel">
<span>Cancel upload</span>
</button>
<button type="button" class="btn btn-danger delete">
<span>Delete</span>
</button>
<input class="toggle" type="checkbox" />
<!-- The global file processing state -->
<span class="fileupload-process"></span>
</div>
&#13;
答案 0 :(得分:0)
看起来您的文件路径不正确。您已将路径设置为
filePath = "C:\\\\Users\\\\Me\\\\Desktop\\\\Capture.JPG"
当你在SendKeys()中设置这些路径时,它变成了:
filePath = "C:\\Users\\Me\\Desktop\\Capture.JPG"
这是无效的路径(因为我尝试了相同的路径&amp;它抛出了无效的路径错误)
请您将文件路径更新为&amp;再试一次:
filePath = "C:\\Users\\Me\\Desktop\\Capture.JPG"
我认为它应该有用,请尝试让我知道它是否有效。如果有任何错误抛出,请发布错误以供分析。
当您要求使用Javascript代码时,请在下面的代码中尝试:
String script = "document.getElementById('files[]').value='" + "C:\\\\Users\\\\Me\\\\Desktop\\\\Capture.JPG" + "';";
((IJavaScriptExecutor)driver).ExecuteScript(script);