使用 webdriver (C#) 上传文件在 Firefox 中有效,但在 IE 中无效

吉祖斯

我可以使用 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

'innerText'在IE中有效,但在Firefox中不可用

window.open在Firefox中有效,但在IE或Chrome中不起作用

日期构造函数在IE中返回NaN,但在Firefox和Chrome中有效

Rmarkdown flexdashboard值框在Chrome或IE中无法正确呈现(但在FireFox中有效)

日期差异在Firefox和IE中有效,但在Chrome中返回NaN

在Chrome或Firefox中,jquery $ .ajax调用会导致401未经授权的响应,但在IE中有效

内联在 CSS 中的 SVG 在 Chrome/Edge 中有效,但在 Firefox 中无效

为什么按钮内的文件输入在 Firefox 中不起作用,但在 Chrome 中有效?

Moment.js在Chrome上具有有效日期,但在IE或Firefox上无效

代码在Firefox Scratchpad中有效,但在代码中无效

伪元素在 Firefox 中有效,但在 Chrome 中无效,为什么?

解构错误对象在Chrome中有效,但在Firefox中无效。该怎么办?

Selenium 测试用例在 Firefox 中有效,但在 Chrome 中无效 - Headless Setup

jQuery Click 功能在 Firefox 中有效,但在 Chrome/Safari 中无效

水平滚动在IE11上有效,但在Chrome和Firefox上无效

在Kendo Scheduler议程视图上隐藏表格列-在Firefox / Chrome中有效,但在IE11中不起作用

表格不在DIV内(在IE 9中有效,但在Chrome或IE11中无效)

在C语言中有效,但在C ++中无效

存储过程在DB中有效,但在C#代码中无效

查询在 SSMS 中有效,但在 C# 中无效?

以下 c++ 代码在 leetcode 中有效,但在我的 vscode 中无效,为什么?

在csv中搜索值,代码在ipython中有效,但在程序中使用时无效

使用变量的查询在PHPMyAdmin中有效,但在PHP脚本中无效

Api request 在 swagger 和 postman 中有效,但在 js 中无效(使用 axios)

jQuery 脚本在 HTML 中有效,但在外部文件中无效

txt文件写入在模拟器中有效,但在实际设备xcode中无效

单张setstyle更新在控制台中有效,但在文件中无效

getResource(“ path”)在Intellij中有效,但在jar文件中无效

为什么powershell命令在终端中有效但在lnk文件中无效?