无法通过selenium上传文件?

时间:2017-01-23 09:34:43

标签: selenium-webdriver

我正在尝试将csv文件上传到我的应用程序。

我收到以下错误:

  

未知错误:无法聚焦元素(会话信息:   chrome = 55.0.2883.87)(司机信息:chromedriver = 2.25.426924   (649f9b868f6783ec9de71c123212b908bf3b232e),平台= Linux的   3.13.0-107-generic x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:101毫秒

我的代码:

UtilsMethods.element = UtilsMethods.driver.findElement(By.xpath(".//*[@id='dragandrophandler']"));
Thread.sleep(10);
UtilsMethods.element.sendKeys("/home/arima/wrangler_testdata/Telstra/test.csv");

html代码:

<section class="wrangler-product-upload-connect z-depth-1">
<div class="row" style="margin-top:25px;">
<div class="col l12 m12 s12">
<div class="col l12 m12 s12" style="padding: 0px;">
<div class="upload-connect-content">
<div id="upload" class="col s12" style="display: block;">
<input id="fileupload" style="display:none" type="file"/>
<form class="file-uplaod-form" method="post" action="" enctype="multipart/form-data">
<div class="box-content center-align">
<div id="dragandrophandler" style="height:250px;">
<i class="fa fa-cloud-upload" aria-hidden="true"/>
<br/>
  Drag & Drop Files Here
</div>
</div>
</form>
</div>
<div id="test2" class="col s12" style="display: none;">
</div>
</div>
</div>
</section>

1 个答案:

答案 0 :(得分:0)

您需要处理div时尝试将路径发送到<input type="file">元素的文件。请尝试以下:

UtilsMethods.element = UtilsMethods.driver.findElement(By.xpath("//input[@id='fileupload']"));
Thread.sleep(10);
UtilsMethods.element.sendKeys("/home/arima/wrangler_testdata/Telstra/test.csv");