C# Selenium Select 所有列元素如果满足两个条件(多条件)

C# Selenium Select all column elements if two conditions are met (Multiple Conditions)

我正在尝试从 html table td 中检索所有元素,但是当我尝试通过具有多个条件的 xpath 查找元素时,它无法检索 0 个元素,获取没有问题具有单一条件的所有值。到目前为止,我的问题可能是 html 元素的顺序。

这是使用 xpath 的单一条件搜索,效果很好: "//input[@class='managebtn'][@value='repost']"

这就是我在没有预期结果的情况下试图实现的目标:

"//td[contains(text(),'manage')]/div[@class='regular']/form[@class='manage']/input[@class='managebtn'][@value='repost']"

以下htmltable是我想看的

 <tbody aria-live="polite" aria-relevant="all">
  <tr class="posting-row odd ui-state-default" role="row">
     <td class="status expired">
        <small class="gc" style="background:">
        Expired               </small>
     </td>
     <td class="buttons expired"></td>
  </tr>
  <tr class="posting-row even ui-widget-content" role="row">
     <td class="status deleted">
        <small class="gc" style="background:">
        Deleted               </small>
     </td>
     <td class="buttons deleted">
        <div class="regular">
           <form action="https://" method="GET" class="manage display">
              <input type="hidden" name="action" value="display">
              <input type="submit" name="go" value="display" class="managebtn">
           </form>
           <form action="https://" method="GET" class="manage ">
              <input type="hidden" name="action" value="repost">
              <input type="submit" name="go" value="repost" class="managebtn">
           </form>
        </div>
     </td>
   
  </tr>
  <tr class="posting-row odd ui-state-default" role="row">
     <td class="status deleted">
        <small class="gc" style="background:">
        Deleted               </small>
     </td>
     <td class="buttons deleted">
        <div class="regular">
           <form action="https://" method="GET" class="manage display">
              <input type="hidden" name="action" value="display">
              <input type="submit" name="go" value="display" class="managebtn">
           </form>
           <form action="https://" method="GET" class="manage ">
              <input type="hidden" name="action" value="repost">
              <input type="submit" name="go" value="repost" class="managebtn">
           </form>
        </div>
     </td>
  </tr>
  <tr class="posting-row even ui-widget-content" role="row">
     <td class="status deleted">
        <small class="gc" style="background:">
        Deleted               </small>
     </td>
     <td class="buttons deleted">
        <div class="regular">
           <form action="https://" method="GET" class="manage display">
              <input type="hidden" name="action" value="display">
              <input type="submit" name="go" value="display" class="managebtn">
           </form>
           <form action="https://" method="GET" class="manage ">
              <input type="hidden" name="action" value="repost">
              <input type="submit" name="go" value="repost" class="managebtn">
           </form>
        </div>
     </td>
    
  </tr>

我只想在第一列的值为 Expired.
时从第二列 (value='repost') 检索所有值 看起来因为这个 table 在列中有多个元素,也许我忽略了一些东西。

因为列有这样的:

      <th data-column="0" class="tablesorter-header tablesorter-headerUnSorted ui-widget-header ui-corner-all ui-state-default" tabindex="0" scope="col" role="columnheader" aria-disabled="false" unselectable="on" style="user-select: none;" aria-sort="none" aria-label="status: No sort applied, activate to apply an ascending sort">
   <div class="tablesorter-wrapper" style="position:relative;height:100%;width:100%">
      <div class="tablesorter-header-inner">
         status <i class="tablesorter-icon ui-icon ui-icon-carat-2-n-s"></i>
      </div>
   </div>
</th>
<th class="sorter-false tablesorter-header tablesorter-headerUnSorted ui-widget-header ui-corner-all ui-state-default" data-column="1" scope="col" role="columnheader" aria-disabled="true" unselectable="on" style="user-select: none;" aria-sort="none" aria-label="manage: No sort applied, sorting is disabled">
   <div class="tablesorter-wrapper" style="position:relative;height:100%;width:100%">
      <div class="tablesorter-header-inner">
         manage  <i class="tablesorter-icon ui-icon"></i>
      </div>
   </div>
</th>

我不知道是否可以有这样一行代码来获取我需要的值:

driver.findElement(By.xpath("//th[@class='sorter-false tablesorter-header tablesorter-headerUnSorted ui-widget-header ui-corner-all ui-state-default' and @text()='manage']"));

这是 table 的样子:

statusExpired 的项目中将具有 value 属性的元素识别为 repost 你必须诱导 for the VisibilityOfAllElementsLocatedBy() and you can use the following based :

new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//tr[contains(@class, 'posting-row')][./td[@class='status expired']]//td[@class='buttons expired']//input[@class='managebtn' and @value='repost']")));

使用 SeleniumExtras.WaitHelpers 你可以使用:

new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//tr[contains(@class, 'posting-row')][./td[@class='status expired']]//td[@class='buttons expired']//input[@class='managebtn' and @value='repost']")));