【问题标题】:Click button inside of table using Following-Sibling PYTHON使用 Follow-Sibling PYTHON 单击表内的按钮
【发布时间】:2017-05-04 18:03:16
【问题描述】:

我有一个 4 行 4 列的表格。最后两列有两个按钮:开始取消。我想为第二行/最后一列按下取消按钮。每一行的代码都是相似的......减去我要启动的服务名称。

<table class="table">
    <tbody>
      ---this is the first row---
       <tr ng-repeat="toyService in theService" ng-class="{'text-muted':toyservice.notFound" class="ng-scope">
      --- this is the second row.. the row I am trying to start----
       <tr ng-repeat="toyService in theService" ng-class="{'text-muted':toyservice.notFound" class="ng-scope">
            <td style="text-align:center">...</td>
            <td class="ng-binding"> Toy Service 2 </td>
            <td class="ng-binding">...</td>
            <td class="toy-service-button-panel text-right">
               ----the first button (Start Button)
                <button type="submit" class="btn btn-info">...</button>
                ----the button I am trying the click (Cancel)
                <button type="submit" class="btn btn-success btn-sm"
                     <span title="Cancel Service">..</span>

每一行都有相同的代码减去服务名称:

第 1 行 玩具服务 1

第 2 行 玩具服务 2

第 3 行 Toy Service 3

第 4 行 玩具服务 4

我尝试的是:

driver.find_element_by_xpath("//td[contains(text(), 'Toy Service 2')]/follow-sibling::td").find_element_by_css_selector("td[class='toy-service-button-panel.text-right']").find_element_by_css_selector("button[class='btn.btn-success.btn-sm']").click()

我收到一条错误提示

无法执行'评估'文档:字符串 //td[contains(text(), Toy Service 1')]/follow-sibling::td' 不是 有效的 XPath 表达式。

我尝试了不同的方法,但我似乎无法单击第 2 行的取消按钮

【问题讨论】:

    标签: python css xpath css-selectors siblings


    【解决方案1】:

    不是follow-sibling,是following-sibling

    driver.find_element_by_xpath("//td[contains(text(), 'Toy Service 2')]/following-sibling::td")
    

    但是,我认为您的方法行不通,因为 following-sibling::td 将匹配 &lt;td class="ng-binding"&gt; Toy Service 2 &lt;/td&gt; 之后的下一个 td 元素,其中不包含所需的按钮。

    相反,我会首先找出您要使用的 tr 元素:

    row = driver.find_element_by_xpath("//tr[td[contains(., 'Toy Service 2')]]")
    

    然后,我会在这一行内操作:

    row.find_element_by_css_selector("td.toy-service-button-panel button.btn.btn-success").click()
    

    我还稍微简化了我们的选择器,无需检查完整的 class 属性值 - 您可以使用点符号检查各个类。

    【讨论】:

      猜你喜欢
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2021-03-02
      • 1970-01-01
      • 2021-01-31
      • 2020-08-06
      • 2017-04-22
      相关资源
      最近更新 更多