how to exactly match using :class selector in watir-webdriver
-
26-10-2019 - |
Question
Regarding the backwards-incompatible change of Watir 0.5.x Finding elements by :class now matches partial class attributes.
How do I get the old behavior? I.e. to match exact class name.
It is breaking my code, in one case:
# expand all multiple records
while $browser.div(:class => "expander").exists?
$browser.div(:class => "expander").click
end
because once .click
ed, the div
's class becomes "expander hasChildren" (and possibly other classes, depending on table row's color and content). Is there a better way to write that code that expands all rows that are still collapsed?
Solution
To match the exact class name, you could use a regex with starting and ending anchors:
$browser.div(:class => /^expander$/).exists?
This would match exactly "expander" but not "expander hasChildren" or "something expander".
For your other question regarding if there is a better way to expand all rows, I think it really depends on what the HTML for the page looks like. Do you have an example? When I had a similar problem with a similar control, I was able to use the structure of the divs around the clickable ones to make the search more specific.