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哪個硒/驅動程序/瀏覽器版本正在使用? –

相關問題