我可以使用 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>
看起来您的文件路径不正确。您已将路径设置为
filePath = "C:\\\\Users\\\\Me\\\\Desktop\\\\Capture.JPG"
当你在 SendKeys() 中设置这些路径时,它变成了这样:
filePath = "C:\\Users\\Me\\Desktop\\Capture.JPG"
这是无效路径(因为我尝试了相同的路径并抛出无效路径错误)
您能否将您的文件路径更新为并重试:
filePath = "C:\\Users\\Me\\Desktop\\Capture.JPG"
我认为,它应该有效,请尝试让我知道它是否有效。如果有任何错误抛出,请发布错误以供分析。
当您要求提供 Javascript 代码时,请尝试提供以下代码:
String script = "document.getElementById('files[]').value='" + "C:\\\\Users\\\\Me\\\\Desktop\\\\Capture.JPG" + "';";
((IJavaScriptExecutor)driver).ExecuteScript(script);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句