简体   繁体   English

无法使用带有 selenium 的 xpath 在表中定位元素

[英]Unable to locate element inside a table using xpath with selenium

I want to click on a button inside my table each row has a update button I want to click on a specfic button inside my table.我想单击表格内的一个按钮,每行都有一个更新按钮,我想单击表格内的特定按钮。

Here is a what my table looks like:这是我的桌子的样子:

<table _ngcontent-vhp-c82="" datatable="" id="dtOptionsComments" class="display table table-striped table-bordered dt-responsive dataTable dtr-inline" aria-describedby="dtOptionsComments_info" style="width: 100%;" width="100%">
  <thead _ngcontent-vhp-c82="">
    <tr _ngcontent-vhp-c82="">
      <th _ngcontent-vhp-c82="" class="no-marking sorting_disabled" rowspan="1" colspan="1" style="width: 50.4px;" aria-label=""></th>
      <th _ngcontent-vhp-c82="" class="sorting sorting_asc" tabindex="0" aria-controls="dtOptionsComments" rowspan="1" colspan="1" style="width: 1109.4px;" aria-label="Comment.Comment Shipping.ShippingDatatable.aria.sortDescending" aria-sort="ascending">Comentario Shipping.Shipping</th>
    </tr>
  </thead>
  <tbody>
    <tr class="odd">
      <td class="no-marking dtr-control">
        <a href="javascript:void(0);">
          <span data-toggle="modal" data-target="#update-modal" update-comment-text="6 MESES DE GARANTIA" update-comment-id="5" class="material-icons md-18 clickable"> edit </span>
        </a>
        <a href="javascript:void(0);">
          <span data-toggle="modal" data-target="#delete-modal" delete-comment-id="5" class="material-icons clickable">delete</span>
        </a>
      </td>
      <td class="sorting_1">6 MESES DE GARANTIA</td>
    </tr>
    <!-- MORE ROWS!!! -->
  </tbody>
  <tfoot _ngcontent-vhp-c82="">
    <tr _ngcontent-vhp-c82="">
      <td _ngcontent-vhp-c82="" class="no-marking" rowspan="1" colspan="1">
        <a _ngcontent-vhp-c82="" href="javascript:void(0);">
          <span _ngcontent-vhp-c82="" class="material-icons clickable"> add_box </span>
        </a>
      </td>
      <td _ngcontent-vhp-c82="" rowspan="1" colspan="1">
        <input _ngcontent-vhp-c82="" formcontrolname="addComment" type="text" id="addComment" name="addComment" class="form-control ng-untouched ng-pristine ng-invalid">
        <!---->
      </td>
    </tr>
  </tfoot>
</table>

Here is my code trials:这是我的代码试验:

IWebElement btnUpdate = _driver.FindElement(By.XPath("//*[update-comment-id='" + commentAction.GetLastQuoteInsertId().ToString() + "']"));
btnUpdate.Click();

I have validated that the function GetLastQuoteInsertId returns the proper value我已验证 GetLastQuoteInsertId 函数返回正确的值

Why is my xPath selector wrong how can I fix it thank you for your help.为什么我的 xPath 选择器错误,我该如何解决,谢谢您的帮助。

You were almost there.你快到了。 While considering a xpath the attribute_name should be always preceded by a @ sign.在考虑 xpath 时, attribute_name应该始终以@符号开头。

Additionally to make the xpath more canonical as the element is a <span> element you can mention //span to start the xpath.此外,为了使 xpath 更加规范,因为元素是<span>元素,您可以提及//span来启动 xpath。

Effectively, your line of code will be:实际上,您的代码行将是:

IWebElement btnUpdate = _driver.FindElement(By.XPath("//span[@update-comment-id='" + commentAction.GetLastQuoteInsertId().ToString() + "']"));
btnUpdate.Click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM