问题
I have a bunch of li
elements. They are either with uncompleted
, 'current' or completed
class or without a class.
How do I find all li
elements without completed
class?
So far I am doing this by selecting necessary li
objects from collection of li
(through calling #attribute_value('class')
, but maybe there is some elegant locating strategy in Watir-WebDriver?
UPD: As long as there is some misunderstanding of the question.
I want to know if there is locating strategy within Watir-WebDriver to find elements which class is not completed
.
I know I can do this with Ruby and doing it like this:
browser.lis.select do |li|
li.attribute_value('class') != 'completed'
end
But the question is if I can do this in one line by passing some argument to #lis
UPD2: Just realized that given class names narrows solutions. Updated question and sorry for that.
回答1:
The LI collection supports locators, which means you can do:
browser.lis(:class, 'uncompleted').each{ |x|
puts x.text
}
UPDATE: For the case where there are multiple classes, you can modify the above to use a regex to check for not completed:
browser.lis(:class, /^(?!completed$)/).each{ |x| puts x.class_name }
This returns all li
that have no class or are not exactly 'completed' (ex 'completed2').
Note: I think .class_name
may have better support than attribute_value('class')
(which I believe does not work in IE as it needs to be className).
回答2:
In order to not assume that there are only two classes of arrays, you can do:
all = browser.lis.collect { |li| li.class }
completed = browser.lis(:class, 'completed').collect { |li| li.class }
not_completed = (all - completed)
or even:
all = browser.lis.collect { |li| li.class }
not_completed = Array.new
all.each do |li|
if li.class != "completed"
not_completed << li
end
end
来源:https://stackoverflow.com/questions/9683853/watir-webdriver-find-elements-which-class-is-not-completed