Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css or xpath selector: elements which have any attribute with specific value

I want to match all elements in an HTML Dom tree which have any attribute with the value "foo". It should either be a CSS or a XPath selector.

My naive approach would be something like this as css selector:

*[*='foo']

How is the correct syntax?

like image 269
Alp Avatar asked Apr 12 '11 11:04

Alp


People also ask

Which attribute of a selector is specified with 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".

Is it better to use CSS selector or XPath?

Advantages and disadvantages of CSS SelectorsPerformance is the same or faster compared to XPath. Easier to learn than XPath, easier to use. CSS Selector only allows unidirectional flow. Using a CSS Selector, we can only traverse from parent to child but not from the child to parent, which is possible with XPath.

What is attribute and value in XPath?

XPath Tutorial from basic to advance level. This attribute can be easily retrieved and checked by using the @attribute-name of the element. @name − get the value of attribute "name". <td><xsl:value-of select = "@rollno"/></td> Attribute can be used to compared using operators.

Which is the correct css3 code to select the HTML element based on their attribute value?

The [attribute=value] selector is used to select elements with the specified attribute and value.


1 Answers

CSS does not define an attribute selector that takes a wildcard as its name.

XPath, however, does. The following expression should work:

//*[@*="foo"]
like image 117
BoltClock Avatar answered Sep 30 '22 13:09

BoltClock