0

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>
Gleb Omarov
  • 45
  • 1
  • 9

2 Answers2

1

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

Dimitrius Lachi
  • 1,277
  • 2
  • 9
  • 21
  • I can't change HTML. – Gleb Omarov Nov 29 '18 at 12:32
  • You can use the function `element.isDisplayed()`, does returns false if element have `display: none` or opacity: 0`, i found it in another question: https://stackoverflow.com/questions/2646195/how-to-check-if-an-element-is-visible-with-webdriver – Dimitrius Lachi Nov 29 '18 at 12:36
0

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?
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352