【问题标题】:How to select visible element using XPath or CSS?如何使用 XPath 或 CSS 选择可见元素?
【发布时间】:2014-10-09 18:04:56
【问题描述】:

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

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

我在 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 对吗?

【问题讨论】:

    标签: javascript css xpath internet-explorer-8 selenium-webdriver


    【解决方案1】:

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

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

    相同的表达式具有相同的结果,但正如这里提到的Xpath does not work with Selenium,Selenium 可能在 XPath 中评估 and 时遇到问题。

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

    【讨论】:

    • 不匹配 display: none(包括空格)或 display: none;display:none;
    【解决方案2】:

    使用 xpath 选择元素:

    选择第一个元素:

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

    选择第二个元素:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 2021-12-22
      • 2020-03-05
      • 2023-01-29
      • 1970-01-01
      相关资源
      最近更新 更多