Select an Element with Empty Class Attribute (Class="") Using CSS

Select an element with empty class attribute (class= ) using CSS?

You can use element-attribute selector here with an empty class value

div[class=""] {
color: red;
}

Demo

Note: You can replace the div with required element

how do I pick a link with empty class using cssSelector - Java Selenium

You should be able to use "a[class='']".

Is there a CSS selector for element without any class?

With section:not([class]) you select every section without the class attribute. Unfortunately, it won't select those sections with an empty class attribute value. So in addition, we have to exclude these sections:

section:not([class]) { /* every section without class - but won't select Section C */
color: red;
}

section[class=""] { /* selects only Section C */
font-weight: bold;
}
<section>Section A</section>
<section class="special">Section B</section>
<section class="">Section C</section>

Using empty CSS class to find HTML elements

It is perfectly fine to locate DOM objects in this manner. Both classes and IDs can be used for DOM element manipulation. It's not a dirty workaround.

Creating elements with empty selectors

Actually, there is a way to select an element with class=""

For example for this

<div class=""></div>

Use

div[class=""]

Working demo: http://jsfiddle.net/JRP4n/

It seems that a class with an empty or null value is different than not having a class attribute defined at all.

So div[class=""] works because it truly targets a defined class which is empty.

But as mentioned in the comments, an empty class attribute is not compliant with the specifications, but interestingly it has been working in all major browsers so far. It also means it "may" stop working should any browser decides to drop support for it.

How to find elements by class

You can refine your search to only find those divs with a given class using BS3:

mydivs = soup.find_all("div", {"class": "stylelistrow"})

Why is xpath not selecting elements with empty classes on using `not`?

Your XPath assumes that node has class attribute and it is not equal to "heisenberg"

Try this one to select node that doesn't contain specific @class

tr[not(@class="heisenberg")]

If node can contain multiple class names you can also try

tr[not(contains(@class, "heisenberg"))]

Can I write a CSS selector selecting elements NOT having a certain class or attribute?

Typically you add a class selector to the :not() pseudo-class like so:

:not(.printable) {
/* Styles */
}

:not([attribute]) {
/* Styles */
}

But if you need better browser support (IE8 and older don't support :not()), you're probably better off creating style rules for elements that do have the "printable" class. If even that isn't feasible despite what you say about your actual markup, you may have to work your markup around that limitation.

Keep in mind that, depending on the properties you're setting in this rule, some of them may either be inherited by descendants that are .printable, or otherwise affect them one way or another. For example, although display is not inherited, setting display: none on a :not(.printable) will prevent it and all of its descendants from displaying, since it removes the element and its subtree from layout completely. You can often get around this by using visibility: hidden instead which will allow visible descendants to show, but the hidden elements will still affect layout as they originally did. In short, just be careful.



Related Topics



Leave a reply



Submit