简体   繁体   English

如何使用Selenium Webdriver在页面上查找仅可见元素?

[英]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? 我试图使用@driver.find_element(:class, 'some class').displayed? but it returns false , when visible span not first with that class on DOM. 但是如果可见范围不是DOM上的该类的首位,它将返回false Is any way I can handle that? 有什么办法可以解决吗?

UPDATE UPDATE

HTML 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: 除了使用style="display: none"您还可以使用CSS类来不显示以下元素:

.hidden { 
   display: none;
}

Now you can check if the element have this class. 现在,您可以检查元素是否具有此类。

UPDATE 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 如果您无法更改HTML,则可以使用element.isDisplayed()函数,如果元素具有display: none ,则返回false display: noneopacity: 0 ,我在另一个问题中找到它: 如何检查元素是否可见与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: 要检查页面上是否显示了任何元素,即使根据提供的HTML DOM ,可见的<span>不在父节点中位于第一个位置,您也可以使用以下解决方案:

  • Using XPath : 使用XPath

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

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

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