CSS selectors to exclude by presence of attributes

There are two styles of td tags in the analyzed document (among other td tags):

 <td align="right" nowrap bgcolor="#A1C87A">...</td> <td align="right" nowrap>...</td> 

How to write a selector that selects only the second type and exclude all td tags?

 document.css('td:not([bgcolor="#A1C87A"])') 

excludes the first type, includes the second type plus all other td tags.

 document.css('td[align="right"][nowrap]') 

excludes all other td tags, but includes both types above.

+7
html css css-selectors nokogiri
source share
3 answers

You can simply combine the :not selector with other attribute selectors:

 document.css('td:not([bgcolor="#A1C87A"])[align="right"][nowrap]') 

You can even put :not after others (it should not be next to the element name):

 document.css('td[align="right"][nowrap]:not([bgcolor="#A1C87A"])') 

They will select all td elements that have bgcolor="#A1C87A" and nowrap , but do not have align="right" , which is after.

+9
source share

td:not([align="right"][nowrap][bgcolor]) should be enough for what you want

0
source share
 td[nowrap][bgcolor] ~ td[nowrap] { code }; 
0
source share

All Articles