Watir에서는 win32ole 객체에 직접 액세스하여 font-weight 요소에 액세스 할 수 있습니다.
ie.div(:index, 1).document.currentStyle.fontWeight
이 당신이 다음의 fontWeight를이 무엇인지 확인 페이지의 모든 요소를 반복하기 만하면됩니다 것이라고 생각 무엇
http://www.w3schools.com/cssref/pr_font_weight.asp에 설명 된대로 당신에게 무게를 나타내는 숫자를 줄 것이다 : 예를 들어, 요소에있는 텍스트의 양. 당신이하는 방식은 테스트하고있는 페이지에 달려 있습니다.
해결 방법 1 - 모든 텍스트가 된 div에있는 경우 잎 노드입니다 : 모든 텍스트는이 같은 리프 노드에있는 경우
:
<body>
<div style='font-weight:bold'>Bold</div>
<div>Plain</div>
</body>
쉽게 할 수있는 :
bold_text = 0
plain_text = 0
ie.divs.each{ |x|
if x.document.currentStyle.fontWeight >= 700
bold_text += x.text.length
else
plain_text += x.text.length
end
}
해결 방법 2 - 스타일이 상호 작용하거나 여러 요소를 사용하는 경우 :
텍스트가 모두 리프 노드에 있지 않거나 <b>
과 같은 다른 태그를 사용하는 경우 (아래 예제 HTML 참조) 더 복잡한 체크가 필요합니다. 이는 .text가 children 요소를 포함하여 요소의 모든 텍스트를 반환하기 때문입니다. 이 경우
<body>
<div style='font-weight:normal'>
Start
<div style='font-weight:bold'>Bold1</div>
<div style='font-weight:bold'>Bold2</div>
End
</div>
<b>Bold Text</b>
</body>
, 나는 대부분의 경우에 대해 다음 작품을 생각 (하지만 정제를해야 할 수도 있습니다) :
#Counting letters, but you could easily change to words
bold_count = 0
plain_count = 0
#Check all elements, though you can change this to restrict to a particular containing element if desired.
node_list = ie.document.getElementsByTagName("*")
0.upto(node_list.length-1) do |i|
#Name the node so it is easier to work with.
node = node_list["#{i}"]
#Determine if the text for the current node is bold or not.
#Note that this works in IE. You might need to modify for other browsers.
if node.currentStyle.fontWeight >= 700
bold = true
else
bold = false
end
#Go through the childNodes. If the node is text, count it. Otherwise ignore.
node.childNodes.each do |child|
unless child.nodeValue.nil?
if bold
bold_count += child.nodeValue.length
else
plain_count += child.nodeValue.length
end
end
end
end
#Determine number of characters that are bold and not. These can be used to determine your percentage.
puts bold_count
puts plain_count
그것은 매우 Watir과 같은 솔루션이 아니라 희망이 당신의 문제를 해결한다.
지역별 "백분율"? 단어 개수로? –
Word가 작동 할 수 있습니다. –