2013-05-01 49 views
0

我正在使用Phantomjs Driver運行無頭版的webdriver腳本。我在上傳文件時遇到問題,因爲在普通瀏覽器(Firefox或Chrome)上會彈出操作系統對話框,該對話框允許我在我的機器中找到該文件並上傳它。 如何用ghostDriver(Phantomjs Driver)來做到這一點? 謝謝Selenium webdriver java - 用phantomjs驅動程序上傳文件

回答

0

始終標識&與上傳時有關的與「文件」類型的元素進行交互。這將解決你彈出窗口的問題。

例:在我的應用程序,上傳相關的元素有以下DOM -

<a id="uploadFileButtonLink" class="uploadFileButtonLink" href="javascript:void(0)" data-uidsfdc="3" style="display: none;">Upload a file</a> 
<input id="multiFileInput" class="multifile-upload-input-button" type="file" name="chatterFile_upload" multiple="multiple"/> 
<input id="multiUploadBtn" class="btnImportant" type="button" value="Upload Files"/> 

在這種情況下,你可以使用的SendKeys方法「multiFileInput」它的類型是「文件」的。 這種方式適用於所有FF,Chrome &也是無頭瀏覽器。

+0

非常感謝你爲了您的迴應。但是,我的應用程序是用extjs(嵌套跨度和div)編寫的。上傳按鈕旁邊沒有文本區域。將不勝感激其他建議 – user2022966 2013-05-02 16:40:20

0

我有同樣的問題,併發布了相同的問題。當使用sendKeys()方法時,PhantomJS掛起。

他們的問題記錄在這裏 - https://github.com/ariya/phantomjs/issues/10993

一個在這個問題上的意見指出,下面的語句工作 -

(PhantomJSDriver) driver.executePhantomJS("var page = this; page.uploadFile('input[type=file]', 'path to file');"); 

你可以試試上面的解決方案,但它可能會或可能不會工作。

0

此代碼幫我上傳,如果「多」屬性設置:

protected void uploadFile(CharSequence... keys) { 
    if (((WrapsDriver) driver).getWrappedDriver() instanceof PhantomJSDriver) { 
     StringBuffer s = new StringBuffer(keys.length); 
     for (int index = 0; index < keys.length; index++) { 
      s.append(keys[index].toString()); 
     } 
     ((PhantomJSDriver) ((WrapsDriver) driver).getWrappedDriver()).executePhantomJS(
       String.format("var page = this; page.uploadFile(arguments[0], '%s');", s.toString()), getElement()); 
    } else { 
     getElement().sendKeys(keys); 
    } 
} 
0
var webPage = require('webpage'); 
var page = webPage.create(); 
page.uploadFile('input[name=image]', '/path/to/some/photo.jpg'); 

在phantomjs的新版本,你可以上傳文件,這樣 uploadfile

相關問題