如何点击selenium c#中的上传按钮

How to click the upload button in selenium c#

请问如何找到上传按钮的元素并通过以下 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/>

根据您的评论:

yup i wanna click the button as once i uploaded the image the upload button only appear. how can i do that?

您必须使用 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();