简体   繁体   中英

How can I find visible only elements on a page with Selenium Webdriver?

I got a page with couple hidden spans with same class. I need to check, if is one of them displayed on a page. I tried to use @driver.find_element(:class, 'some class').displayed? but it returns false , when visible span not first with that class on DOM. Is any way I can handle that?

UPDATE

HTML

<html>
   <head></head>
   <body>
      <div class="span1" style="display: none;">
        <span class="some class">Some Error</span>
      </div>
      <div class="span2" style="display: none;">
        <span class="some class">Some Error</span>
      </div>
      <div class="span3" style>
        <span class="some class">Some Error</span>
      </div>
   </body>
 </html>

Instead of use style="display: none" you can use a CSS class for not display elements like:

.hidden { 
   display: none;
}

Now you can check if the element have this class.

UPDATE

If you can't change the HTML, may you can use the function element.isDisplayed() , does returns false if element have display: none or opacity: 0 , i found it in another question: How to check if an element is visible with WebDriver

To check if any of the element is displayed on a page even when the visible <span> is not first within the parent node as per the HTML DOM you have provided, you can use the following solution:

  • Using XPath :

     @driver.find_element(:xpath,"//span[@class='some class']").displayed? 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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