如何使用webdriver自动执行bootstrap fileupload上传控件

Nik*_*wal 5 file-upload webdriver

我想自动化文件上传过程,该过程使用内置bootstrap的文件上传控件.我正在使用webdriver做同样的事情.下面是我的代码,但不幸的是它不起作用:

element=driver.findElement(By.xpath("//[@id='upload']/fieldset/div[2]/input[1]"));
element.sendKeys(pathToFile);
Run Code Online (Sandbox Code Playgroud)

它给出了一个element not visible错误.

以下是我试图自动化的bootstrap fileupload控件示例 - 通过JavaScript:在此URL上http://markusslima.github.io/bootstrap-filestyle/ 请参阅下面的样式 -

$(":file").filestyle({icon: false});
Run Code Online (Sandbox Code Playgroud)

Gic*_*ico 2

好的。我想我解决了。

    WebElement fileInput = driver.findElement(By.id("document"));

    JavascriptExecutor js = (JavascriptExecutor) driver;
    WebElement element = driver.findElement(By.id("document"));
    js.executeScript("arguments[0].setAttribute('style', 'left:30px')",
            element);

    fileInput.sendKeys(fileName);
Run Code Online (Sandbox Code Playgroud)

bootstrap-filestyle.js 隐藏了一个输入元素,因此您必须将其移动到可见区域,然后以标准方式设置它。

这么简单的解决方案却这么麻烦。

这是我原来的html代码:

<span id="documentUpload">
    <input type="file" id="document" name="document" class="notMandatory" onkeypress="return noenter(event)" tabindex="-1" style="position: absolute; left: -9999px;">
    <div class="bootstrap-filestyle" style="display: inline;" tabindex="0">
        <input type="text" class="input-xlarge" disabled="" autocomplete="off"> 
        <label for="document" class="btn"><i class="icon-folder-open"></i> <span>Upload</span></label>
    </div>
</span>
Run Code Online (Sandbox Code Playgroud)