简体   繁体   English

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

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

I want to select the apply button in the following code. 我想在以下代码中选择“应用”按钮。 There are two buttons and only one button is visible. 有两个按钮,只有一个按钮可见。

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

I have written above XPath to select the visible one but in web driver it says unable to access the element. 我已经在XPath上方编写了选择可见的XPath,但是在Web驱动程序中它说无法访问该元素。 (browser - IE8) (浏览器-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>

My question is there anyway other work around for this issue. 我的问题是针对此问题还有其他解决方法。 I think there are plenty of ways to write the above xpath am i right? 我认为有很多方法可以编写上述xpath,对吗?

You could try the following in case this is a Selenium issue: 如果这是硒问题,可以尝试以下方法:

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

It's the same expression with the same result, but as mentioned here Xpath does not work with Selenium it's possible that Selenium has an issue with evaluating and in XPath. 这是相同的表达具有相同的结果,但这里所说的XPath不与硒的工作有可能是硒有一些问题与评估and在XPath中。

Another issue I just want to mention is that you shoudn't use the same id for multiple elements, id s should be unique. 我只想提及的另一个问题是,您不应对多个元素使用相同的idid应该是唯一的。 Otherwise your HTML is not valid. 否则,您的HTML无效。 When you change the ids to unique values, it'd be possible to reduce the XPath match conditions. 当您将ID更改为唯一值时,可以减少XPath匹配条件。

I am a bit confused about your question. 我对你的问题有点困惑。

If you are trying to select the button in order to modify its style through CSS, just create the appropriate entry in your CSS file and an ID for the HTML element (the button you need). 如果您试图选择按钮以通过CSS修改其样式,只需在CSS文件中创建适当的条目以及HTML元素的ID(您需要的按钮)即可。

Example in W3: Select HTML element by ID W3中的示例: 通过ID选择HTML元素

Selecting an element using xpath: 使用xpath选择一个元素:

Selecting the first element: 选择第一个元素:

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

Selecting the second element: 选择第二个元素:

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

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

相关问题 使用带有硒的xpath选择元素的文本 - select text of element using xpath with selenium 错误:当相对Xpath正常工作时,使用Dynamic Xpath访问元素时出现不可见元素 - Error: Element is not visible comes up when accessing an element using Dynamic Xpath while the relative Xpath works fine 如何在CSS(使用属性)中选择不是另一个元素的后代的元素 - How select an element in CSS (using attribute) that is NOT a descendant of another element select 如何通过 xpath 元素并用 puppeteer 点击它? - How select an element by xpath and click on it with puppeteer? 如何使用带有 JavaScript 的 XPATH 选择与元素内部文本的一部分匹配的元素 - How to select an element that matches a part of the element's inner text using XPATH with JavaScript 如何使用 Angularjs 在量角器中选择可见元素 - How to select a visible element in Protractor with Angularjs jQuery select 元素由 XPath - jQuery select element by XPath 使用 Xpath 如何 select 一个元素的多个父类具有特定的 innerText? - Using Xpath how to select multiple parent classes of an element that has a specific innerText? 如何使用“旧”(HTML5之前的)HTML控制html select元素中的可见项? - How can I control the visible items in an html select element using “old” (pre-HTML5) HTML? CasperJS无法使用CSS选择器或xPath查找元素 - CasperJS can't find element using CSS selector or xPath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM