Selenium webdriver java - 使用phantomjs驱动程序上传文件

时间:2013-05-01 17:43:16

标签: selenium webdriver ghostdriver

我使用Phantomjs Driver运行selenium webdriver脚本无头。我在上传文件时遇到问题,因为在普通浏览器(firefox或chrome)上它会弹出操作系统对话框,允许我在我的机器中找到该文件并上传它。 如何使用ghostDriver(Phantomjs Driver)做到这一点? 感谢

4 个答案:

答案 0 :(得分:0)

始终识别&在涉及上载时与“文件”类型的元素交互。这将解决您的弹出窗口问题。

Ex:在我的应用程序中,上传相关元素具有以下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”,其类型为“file”。 这样它适用于所有FF,Chrome和&amp;也是无头浏览器。

答案 1 :(得分:0)

我遇到了同样的问题并且发布了同样的问题。使用sendKeys()方法时,PhantomJS挂起。

他们在此处记录了一个问题 - https://github.com/ariya/phantomjs/issues/10993

对该问题的评论之一表明以下陈述有效 -

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

您可以尝试上述解决方案,但它可能会也可能不会。

答案 2 :(得分:0)

如果设置了'multiple'属性,此代码帮助我上传:

ByCharWrapping

答案 3 :(得分:0)

var webPage = require('webpage');   
var page = webPage.create();
page.uploadFile('input[name=image]', '/path/to/some/photo.jpg');

在新版本的phantomjs中,您可以像这样上传文件 uploadfile