【问题标题】:Locate input element and send filepath to upload file找到输入元素并发送文件路径以上传文件
【发布时间】:2016-03-03 10:45:13
【问题描述】:

我正在尝试使用 selenium 网络驱动程序上传文件。 UI 有一个上传按钮,位于 div 标签内。该按钮有一个隐藏的文本框,位于 span 标签内。我能够定位到 span 标签。

<div class="ng-scope" ng-if="showUpload" style="padding-bottom:5px;">
<div class="btn ng-binding" ng-class="class" flow-drop="" style="padding-top:30px;vertical-align:middle;width:370px;height:100px;background-color:#eee;border:1px solid #ccc" ng-style="style" flow-drag-leave="style={border:'1px solid #ccc'}" flow-drag-enter="style={border:'1px solid #007670'}" flow-prevent-drop="">Drag and Drop your files here.</div>
<span class="ng-binding" style="padding:0 20px">or</span>
<span class="btn btn-primary ng-binding" flow-attrs="{accept:'image/*,.pdf,.csv,.txt,.doc'}" flow-btn="">
<i class="icon icon-file"/>
Upload File                       
<input type="file" style="visibility: hidden; position: absolute; width: 1px; height: 1px;" multiple="multiple" accept="image/*,.pdf,.csv,.txt,.doc"/>
</span>
</div>

我可以通过 firepath 找到输入标签,但是使用 java 代码同样不起作用。

WebElement m_upload = driver.findElement(By.CssSelector("input[type='files']")); 返回空值。 谁能告诉我该怎么做?

【问题讨论】:

  • 你检查文件单数和复数文件了吗?
  • 我无法找到输入文本框。只有这样我才能使用 sendKeys() 发送文件。
  • 原因:org.openqa.selenium.NotFoundException:在 VISIBLE 状态下找不到匹配 'By.cssSelector: input[type='files']' 的元素。
  • 是的,我的意思是:在您的 html 中,您的 type="file" 没有 S,而在您的选择器中,您有 type="files" WITH 和 S。
  • type='文件'。我犯了一个错误。但还是不行。

标签: html angularjs selenium


【解决方案1】:

试试下面的代码

//Give your file path here
  var orgFile = @"C:\Users\username\Downloads\pdf.pdf";
//get uploadfile input and send pat using SendKeys 
            var uploadFile = driver.FindElement(By.CssSelector("input[type='file']"));
            uploadFile.SendKeys(orgFile);

如果有任何问题,请告诉我。

【讨论】:

    【解决方案2】:

    您不能对隐藏元素执行任何操作。首先,您必须执行一个更改隐藏输入元素可见性的操作。

    另外请记住,页面正在使用 AngularJS,并且您正在使用 Selenium 对其进行测试,因此可能存在同步问题,因此最好使用 Protractor 对其进行测试,或者通过在 selenium 中通过在之前放置隐式或显式等待语句来处理它上传文件。

    我认为它会解决问题。

    【讨论】:

      猜你喜欢
      • 2020-07-11
      • 2016-04-28
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多