0

我想知道如何上传文件到bootstrap-fileinput元素使用Selenium与FirefoxDriver。我试图使用硒上传文件bootsrap-fileinput与geckodriver

WebElement input = letter.findElement(By.cssSelector("#letter input")); 
input.sendKeys("/home/me/loremIpsum.pdf"); 

我得到

org.openqa.selenium.InvalidArgumentException: File not found: /home/me/loremIpsum.pdf 

当然的文件/home/me/loremIpsum.pdf确实存在。

相同的代码使用chromedriver。

我放在一起的jsfiddle展现的FileInput按钮:https://jsfiddle.net/yscgx2zc/

从我的应用程序(从Firefox开发者控制台复制)所提供的HTML看起来像这样。找到接近底部的input元素。

<div id="letter" class="form-group"><label class="control-label">The Letter<span>*</span></label><div class="file-input file-input-new"><div class="file-preview "> 
    <div class="close fileinput-remove">×</div> 
    <div class="file-drop-disabled"> 
    <div class="file-preview-thumbnails"> 
    </div> 
    <div class="clearfix"></div> <div class="file-preview-status text-center text-success"></div> 
    <div class="kv-fileinput-error file-error-message" style="display: none;"></div> 
    </div> 
</div> 
<div class="kv-upload-progress hide"><div class="progress"> 
    <div class="progress-bar progress-bar-success progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%;"> 
     0% 
    </div> 
</div></div> 
<div class="input-group file-caption-main"> 
    <div tabindex="500" class="form-control file-caption kv-fileinput-caption"> 
    <div class="file-caption-name"></div> 
</div> 

    <div class="input-group-btn"> 
     <button type="button" tabindex="500" title="Clear selected files" class="btn btn-default fileinput-remove fileinput-remove-button"><i class="glyphicon glyphicon-trash"></i> <span class="hidden-xs">Remove</span></button> 
     <button type="button" tabindex="500" title="Abort ongoing upload" class="btn btn-default hide fileinput-cancel fileinput-cancel-button"><i class="glyphicon glyphicon-ban-circle"></i> <span class="hidden-xs">Cancel</span></button> 

     <div tabindex="500" class="btn btn-primary btn-file"> 
     <i class="glyphicon glyphicon-folder-open"></i>&nbsp; <span class="hidden-xs">Browse …</span> 
     <input data-show-upload="false" data-allowed-file-extensions="[&quot;pdf&quot;]" data-allowed-file-types="[&quot;pdf&quot;]" accept="application/pdf" class="file" id="1502961793221" type="file"></div> 
    </div> 
</div></div></div> 
+0

不知何故,我无法找到问题中提供的HTML中的'tagName(“input”)''。我错过了什么吗? – DebanjanB

+0

@DebanjanB谢谢,我编辑了源代码,使它更容易找到(在底部)。 – bastian

+0

可能是[这个bug](https://github.com/mozilla/geckodriver/issues/858)。 – bastian

回答

0

您可以用下面的代码块尝试:

WebElement input = letter.findElement(By.xpath("//input[@id='1502957288010']")); 
input.sendKeys("/home/me/loremIpsum.pdf"); 
+0

谢谢,我再次更新了我的问题。输入元素已经找到。不幸的是它不起作用。 – bastian

0

只是试图文件上载到http://plugins.krajee.com/file-basic-usage-demo,这是指定的引导文件输入演示页。

适用于Firefox和Chrome。唯一的问题是输入可见性。当我看到它时,我能够上传文件。

但是,您的错误消息对我来说似乎很明显。您的文件在磁盘上找不到。请描述你的环境。你使用本地或远程WebDriver?与远程的情况一样,这个文件应该存在于启动浏览器的目标虚拟机上。

+0

我在Linux系统上测试Firefox。当我在错误消息中指定的文件上执行'ls'(复制粘贴)时,找到它。你是如何使它可见的。我试过'js.executeScript(“arguments [0] .style.visibility ='visible'”,input);' – bastian

+0

对于演示示例,我一直使用jQuery去除父元素的类,它隐藏了所需的输入:'executeScript “$(\”。btn.btn-primary.btn-file \“)。removeClass(\”btn-file \“);”);' –

+0

@bastian哪个硒/驱动程序/浏览器版本正在使用? –

相关问题