Python Selenium - 如何根据另一个DIV的文本单击div中的按钮

时间:2017-05-15 06:11:42

标签: python selenium

使用Python Selenium,我想根据另一个div点击“Add ...”按钮。

我无法将xpath following::div视为发现here,因为它们位于不同的div中。

基本上,查找第一个div文本以包含一些文本并基于它,我想点击第四个div上的“Add ...”按钮。这是关于Divs的一系列,你可以在屏幕截图中看到。

screenshoot

在下面的示例/代码中,我想找到“模板1”< - xpath //*[@id="app"]/div/div[3]/div/div[24]/div[1]/div/h3/text()[1]

并以此为基础

我想点击“添加...”按钮< - xpath //*[@id="app"]/div/div[3]/div/div[24]/div[4]/div/table/thead/tr/th[3]/button

<div style="margin-bottom: 50px;" class="">
   <div>
      <div>
         <h3>
            <!-- react-text: 1148 -->Template 1<!-- /react-text --><!-- react-text: 1149 -->&nbsp;<!-- /react-text -->
         </h3>
      </div>
   </div>
   <div class="btn-group btn-table-action pull-right" style="vertical-align: top; top: -32px;"><button type="button" class="btn btn-link"><i class="fa fa-pencil-square-o" style="color: rgb(51, 122, 183);"></i></button><button type="button" class="btn btn-link"><i class="fa fa-trash-o" style="color: rgb(217, 83, 79);"></i></button></div>
   <div class="detail-pane" style="background-color: rgb(245, 245, 245); border-radius: 4px; font-size: 90%; padding: 4px 20px; margin-bottom: 14px;">
      <div>
         <h5></h5>
         <dl class="dl-horizontal">
            <span>
               <dt style="font-weight: normal;">Key</dt>
               <dd style="font-weight: normal;">template-1-key</dd>
            </span>
         </dl>
      </div>
   </div>
   <div>
      <h5 style="color: rgb(95, 94, 94); margin-top: 27px;">Templates</h5>
      <div class="detail-pane" style="background-color: rgb(245, 245, 245); border-radius: 4px; font-size: 90%; padding: 4px 20px; margin-bottom: 14px;">
         <table class="table table-hover" style="table-layout: fixed;">
            <thead>
               <tr>
                  <th>Language</th>
                  <th>Asset Path</th>
                  <th>
                     <button type="button" class="btn btn-primary pull-right btn-xs" style="margin-left: 6px;">
                        <span>
                           <i class="fa fa-plus-square-o">&nbsp;</i><!-- react-text: 1174 -->Add...<!-- /react-text -->
                        </span>
                     </button>
                  </th>
               </tr>
            </thead>
            <tbody>
               <tr>
                  <td colspan="3">No entries found so far...</td>
               </tr>
            </tbody>
         </table>
      </div>
   </div>
</div>

1 个答案:

答案 0 :(得分:1)

尝试下面的xpath,让我知道它是否适合你

.//div/h3[text()[normalize-space() = 'Template 1']]/../../following-sibling::div/div//table//tr/th/button[@type='button']

说明:

.//div/h3[text()[normalize-space() = 'Template 1']]   // To locate element with "Template 1" text

/../..     // to navigate the parent node for template 1 node

following-sibling::div/div//table//tr/th/button[@type='button']  // Locate the button node which is sibling node  of parent div of Template 1 text