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

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

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

//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,对吗?

最佳答案

如果这是 Selenium 问题,您可以尝试以下操作:

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

这是具有相同结果的相同表达式,但正如此处提到的 Xpath does not work with Selenium Selenium 可能在 XPath 中计算 时出现问题。

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

关于javascript - 如何使用 XPath 或 CSS 选择可见元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26285118/

相关文章:

javascript - 如何多次替换同一个字符?

javascript - 防止滚动但 div 需要位置 :absolute

javascript - 是否有包含 String.prototype.format 或等效项的流行 JavaScript 库?

javascript - 侧边栏推送内容

css - 使用@keyframes 的双向动画

xpath - 用于数字比较的xpath表达式

python - 如何使用 Selenium 和 Python 等待元素包含属性样式 ="display:none;"

python - 使用 Selenium 和 Python 通过单击而不使用 url 下载文件

javascript - 从 javascript 代码触发 ngFx 动画

javascript - Safari 中的 ReactDom 中缺少 "minLength"