加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

file-upload – 如何使用webdriver自动执行bootstrap fileupload

发布时间:2020-12-17 20:42:57 所属栏目:安全 来源:网络整理
导读:我想自动化文件上传过程,该过程使用内置bootstrap的文件上传控件. 我正在使用webdriver做同样的事情. 下面是我的代码,但不幸的是它不起作用: element=driver.findElement(By.xpath("//[@id='upload']/fieldset/div[2]/input[1]"));element.sendKeys(pathToF
我想自动化文件上传过程,该过程使用内置bootstrap的文件上传控件.
我正在使用webdriver做同样的事情.
下面是我的代码,但不幸的是它不起作用:

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

它给出了一个元素不可见的错误.

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

$(":file").filestyle({icon: false});

解决方法

好.我想我解决了.

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);

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读