繁体   English   中英

如何使用XPath或CSS选择可见元素?

[英]How to select visible element using XPath or CSS?

我想在以下代码中选择“应用”按钮。 有两个按钮,只有一个按钮可见。

//input[@value='Apply' and @id='btn' and @name='btn' and not(ancestor::td[contains(@style,'display:none')])]

我已经在XPath上方编写了选择可见的XPath,但是在Web驱动程序中它说无法访问该元素。 (浏览器-IE8)

<table class="ColumnTable" cellspacing="0">
   <tbody>
      <tr>
         <td>
            <div id="dashboard~120" class="Section" style="" headeron="" minimized="false" rendered="false">
               <table class="SectionT" cellspacing="0" cellpadding="0">
                  <tbody>
                     <tr>
                        <td style=" display:none;">
                           <div id="dashboard~Contents" style="">
                              <table style="width:100%">
                                 <tbody>
                                    <tr height="100%">
                                       <td class="EItem" valign="TOP" align="CENTER" colspan="2" style="">
                                          <div id="EmbedViewd" reloadinline="">
                                             <div id="NavDone" style="display:;">
                                                <div id="Result" result="Prompt">
                                                   <table class="ViewTable" cellspacing="0">
                                                      <tbody>
                                                         <tr>
                                                            <td>
                                                               <div id="newLayout">
                                                                  <form style="margin: 0;" method="post" action="javascript:void(null);">
                                                                     <div style="">
                                                                        <table class="PromptView" style="">
                                                                           <tbody>
                                                                              <tr>
                                                                                 <td class="ButtonsCell">
                                                                                    <input id="btn" class="button" type="button" tabindex="0" value="Apply" name="btn" style="background-color: rgb(240, 240, 240);">
                                                                                 </td>
                                                                              </tr>
                                                                           </tbody>
                                                                        </table>
                                                                     </div>
                                                                  </form>
                                                               </div>
                                                            </td>
                                                         </tr>
                                                      </tbody>
                                                   </table>
                                                </div>
                                             </div>
                                          </div>
                                       </td>
                                    </tr>
                                 </tbody>
                              </table>
                           </div>
                        </td>
                     </tr>
                  </tbody>
               </table>
            </div>
         </td>
      </tr>
      <tr>
         <td>
            <div id="dashboard~121" class="Section" style="" headeron="true" minimized="false" rendered="false">
               <table class="SectionT" cellspacing="0" cellpadding="0">
                  <tbody>
                     <tr>
                        <td>
                           <div id="dashboard~Contents" style="">
                              <table class="SectionTD" style="width:100%; border-top:none;">
                                 <tbody>
                                    <tr height="100%">
                                       <td class="EItem" valign="TOP" align="CENTER" colspan="2" style="">
                                          <div id="EmbedViewd" reloadinline="">
                                             <div id="NavDone" style="display:;">
                                                <div id="Result" result="Prompt">
                                                   <table class="ViewTable" cellspacing="0">
                                                      <tbody>
                                                         <tr>
                                                            <td>
                                                               <div id="newLayout">
                                                                  <form style="margin: 0;" method="post" action="javascript:void(null);">
                                                                     <div style="">
                                                                        <table class="PromptView" style="">
                                                                           <tbody>
                                                                              <tr>
                                                                                 <td class="ButtonsCell">
                                                                                    <input id="btn" class="button" type="button" tabindex="0" value="Apply" name="btn" style="background-color: rgb(240, 240, 240);">
                                                                                 </td>
                                                                              </tr>
                                                                           </tbody>
                                                                        </table>
                                                                     </div>
                                                                  </form>
                                                               </div>
                                                            </td>
                                                         </tr>
                                                      </tbody>
                                                   </table>
                                                </div>
                                             </div>
                                          </div>
                                       </td>
                                    </tr>
                                 </tbody>
                              </table>
                           </div>
                        </td>
                     </tr>
                  </tbody>
               </table>
            </div>
         </td>
      </tr>
   </tbody>
</table>

我的问题是针对此问题还有其他解决方法。 我认为有很多方法可以编写上述xpath,对吗?

如果这是硒问题,可以尝试以下方法:

//input[@value='Apply'][@id='btn'][@name='btn']  
       [not(ancestor::td[contains(@style,'display:none')])]

这是相同的表达具有相同的结果,但这里所说的XPath不与硒的工作有可能是硒有一些问题与评估and在XPath中。

我只想提及的另一个问题是,您不应对多个元素使用相同的idid应该是唯一的。 否则,您的HTML无效。 当您将ID更改为唯一值时,可以减少XPath匹配条件。

我对你的问题有点困惑。

如果您试图选择按钮以通过CSS修改其样式,只需在CSS文件中创建适当的条目以及HTML元素的ID(您需要的按钮)即可。

W3中的示例: 通过ID选择HTML元素

使用xpath选择一个元素:

选择第一个元素:

//div[@id='dashboard~120']descendant::input[@id='btn'].Click;

选择第二个元素:

//div[@id='dashboard~121']descendant::input[@id='btn'].Click;

暂无
暂无

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

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