2014-01-20 2 views
2

특정 클래스를 제외하면서 클래스와 일치시킬 수 있기를 원합니다.Watir로 여러 클래스를 일치시키고 제외하십시오

다음과 같은 것을 사용하여 지정된 클래스와 일치하는 모든 li 요소를 가져올 수 있지만 동시에 클래스를 어떻게 제외시킬 수 있는지 잘 모르겠습니다.

b = Watir::Browser.new 
free_boxes = b.lis(:class, /cellGridGameStandard/) 

나는 cellGridGameStandard 클래스 모든 리튬 요소와 일치합니다 뭔가에이를 변경하려면, 또한 notEligible 클래스 또는 ownAlready 클래스 중 하나를 포함하는 모든 요소를 ​​제외합니다.

답변

2

다음은 몇 가지 옵션입니다.

우리가 HTML과 같은 것으로 가정 해 봅시다 :

<ul> 
    <li class="cellGridGameStandard"> 
     Element 1 
    </li> 
    <li class="cellGridGameStandard ownAlready"> 
     Element 2 
    </li> 
    <li class="cellGridGameStandard notEligible"> 
     Element 3 
    </li> 
    <li class="cellGridGameStandard"> 
     Element 4 
    </li> 
</ul> 

첫 번째와 네 번째 리 요소가 지정된 기준과 일치.

matching = browser.lis(:class => 'cellGridGameStandard') 
    .find_all { |li| 
     ['ownAlready', 'notEligible'].none? { 
      |class_name| li.class_name.split.include? class_name 
     } 
    } 
p matching.collect(&:text) 
#=> ["Element 1", "Element 4"] 

쓰기 쉽게하지만 때로는 읽기 어려워 간주 또 다른 옵션은, CSS를 로케이터를 사용하는 것입니다

하나의 옵션은 ownAlready 또는 notEligible 클래스를하지 않아도 문양을 확인하는 것입니다 :

matching = browser.elements(:css => 'li.cellGridGameStandard:not(.ownAlready):not(.notEligible)') 
p matching.collect(&:text) 
#=> ["Element 1", "Element 4"] 
+0

감사합니다; 내가 필요한 것. 나는 css locator 옵션을 사용했다. 멋지고 간결합니다. – bigtunacan

관련 문제