Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a CSS Selector selecting elements NOT having a certain attribute?

How to write a CSS Selector selecting elements NOT having a certain attribute?

I have 2 <div> nodes as follows:

  • First:

    <div class="weEq5" style="will-change; width;">
        <button class="_35EW6">
    
  • Second:

    <div class="weEq5">
        <button class="_35EW6">
    

I need to select the <div> (with the similar class) and each of them which have a similar descending <button> but without the style attribute.

XPath seems working fine as:

//div[@class and not (@style)]/button

I am looking for an equivalent CssSelector.

Trials:

div[class :not(style)]>button (doesn't works).

I have been through the following discussion but they seem to be discarding the class attribute as :not([class]) as in:

  • Can I write a CSS selector selecting elements NOT having a certain class?
  • Is it possible to define in CSS NOT to apply style if element have certain class? [duplicate]

I was looking in similar lines ending with :not(attribute).

like image 414
undetected Selenium Avatar asked Dec 26 '18 04:12

undetected Selenium


People also ask

How do you select an element that does not have a specific class?

To select element that does not have specific class with JavaScript, we can use the document. querySelector method with the :not pseudoclass. const li = document. querySelector("li:not(.

How do you exclude elements in CSS?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.

How can we select elements with a specified attribute in CSS?

The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".


2 Answers

I think more accurate CSS Selector is:

div[class]:not([style])>button

because the button element is a child of div element.

Hope it helps you!

like image 121
Ratmir Asanov Avatar answered Sep 28 '22 04:09

Ratmir Asanov


That's the code you're looking for:

div:not([style]) button{
  background-color: red;
}

Now let's break it down. We have have four selectors in this example:

  1. div and button - these select html elements. We can replace it for example with a class selector like .weEq5.
  2. :not() - indicates that we want everything that does not qualify as the selector inside the brackets.
  3. [style] - an attribute selector which is very powerful. We can place inside the not any other css selector like html tag names (button or div), class names or ids.

The combination of div:not([style]) means that we want all divs that do not have a style attribute. After which we have a space and a button means that we want all the buttons that are inside the above selector.

Adding a > before the button div:not([style]) > button will only select button elements which are direct children of the selected div. It will exclude from selection buttons that are deeper inside the div.

like image 41
Adam Genshaft Avatar answered Sep 28 '22 04:09

Adam Genshaft