How did I miss this? Ruby bindings support CSS compound selectors!

I just came across this … but it has saved me from begging a developer to make a simple DOM tweak to a part of the site that no one really cares too much about.

CSS selectors in general web design allow you to specify multiple selectors, called compound selectors, to apply styling to using a comma between each selector. Why shouldn’t SWD?

Well, today I learned that indeed it does (since the 2.24.0 version from 06-19-2012)!

How do you use this? Well, let’s say you have a portion of the DOM that could have either a “p” or an “a” tag, you could write something like this to select one OR the either :

@driver.find_element(:css => "a, p")

This will select the first element that SWD comes across, regardless of which order you write the selectors. Thus, given this HTML

<div id="outer">
  <span id="inner">Foo</span>
</div>
<span id="last">Bar</div>

You could write a selector

@driver.find_element(:css => "#last, #outer")

And it would select the div with the id of “outer” because it is the first selector that is found in the DOM. Same goes for child descendents. Writing a selector like this

@driver.find_element(:css => "span#inner, #outer")

will select the “outer” div again.

Advertisements