【问题标题】:How to click the upload button in selenium c#如何在 selenium c# 中单击上传按钮
【发布时间】:2018-07-30 04:33:37
【问题描述】:

请问,如何找到上传按钮的元素并通过以下 HTML 代码单击鼠标进行操作:

<div class="uploadImage-wrap">
      <!-- Comment Title -->
      <div class="uploadImageTitle-wrap">
          <h2>Upload Files</h2>
      </div>

      <div id="uploadImage-containerSEC-2">

             <div id="dropzoneplaceSEC-2" class="dz-message">

                <div class="needsclick">
                <i class="fa fa-upload" aria-hidden="true"></i></br>
                Drop files here to upload.<br> or browse for a file

              </div>        
            </div>
           <input name="userFileName" type="hidden" value=""  id="userFileNameSEC-2">
           <input name="issueKey" type="hidden" value=""  id="issueKeySEC-2">
          <a href="#"><button type="button" id="uploadImageButtonSEC-2" class="btn blue changeBtn display-none" style='margin-left:40%;' onclick="addAttachmentForIssue(this)">Upload</button></a>

     </div>
</div><br/>

【问题讨论】:

  • 它有一个id。究竟是什么问题?
  • 是说当我编写此代码时找不到元素 driver.FindElement(By.Id("dropzoneSEC-2")); js.ExecuteScript("arguments[0].scrollIntoView();", Element); driver.FindElement(By.Id("uploadImageButtonSEC-2")).Click();
  • 这个按钮的类是display-none,可能它是不可见的。您确定这是您真正想要点击的按钮吗?
  • driver.FindElement 仅适用于可见项目,这意味着它们应该可见且不被其他元素覆盖

标签: c# selenium selenium-webdriver nunittestadapter


【解决方案1】:

根据您的评论:

是的,我想在上传图片后单击按钮 按钮只出现。我该怎么做?

您必须使用WebDriverWait 等待元素可点击:

// after you have uploaded an image
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementIsClickable(By.Id("uploadImageButtonSEC-2")));
clickableElement.click();

这将等待至少 1 分钟,直到元素可以点击,然后才会点击它。

注意:如果你的 id 是通用的,你可以使用xPath 来定位它:

//button[starts-with(@id, 'uploadImageButton')]

和代码:

// after you have uploaded an image
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementIsClickable(By.Xpath("//button[starts-with(@id, 'uploadImageButton')]")));
clickableElement.click();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-08
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多