What Is the Regex of a CSS Selector

Using regular expression in css?

You can manage selecting those elements without any form of regex as the previous answers show, but to answer the question directly, yes you can use a form of regex in selectors:

#sections div[id^='s'] {
color: red;
}
<div id="sections">
<div id="s1">one</div>
<div id="s2">two</div>
<div id="s3">three</div>
<div id="t1">four</div>
</div>

CSS selector regex anything in between

You could use the ^= ('starts with') and $= ('ends with') attribute selectors.

[class^="dh-"][class$="-bl"]{}

Css with regex for id

You can use special CSS selector for that: id$='0' means id ends with 0 and id^='s' means id begins with s.

#sections div[id$='0'][id^='s'] {    color: red;  }
<div id="sections"><div id="s10">one</div><div id="s2">two</div><div id="s30">three</div><div id="t1">four</div></div>

What is the regex of a CSS selector

So finally I've found a way better way to do it.

I use a LESS editor (eg: http://lesstester.com/)

and simply nest my whole css file:
.mySelector{ your css here }

Is it possible to have a regex in CSS Selector for tagname ?

For attributes, yes, you can. But for elements, you can have the possible values comma separated. W3 CSS Selectors

Selector            Example              Description                                                                              CSS
element1,element2 h1,h2 Selects every <ul> element that are preceded by a <p> element 3
[attribute~=value] [title~=flower] Selects all elements with a title attribute containing the word "flower" 2
[attribute|=value] [lang|=en] Selects all elements with a lang attribute value starting with "en" 2
[attribute^=value] a[href^="https"] Selects every <a> element whose href attribute value begins with "https" 3
[attribute$=value] a[href$=".pdf"] Selects every <a> element whose href attribute value ends with ".pdf" 3
[attribute*=value] a[href*="w3schools"] Selects every <a> element whose href attribute value contains the substring "w3schools" 3

CSS regex selector match one OR another condition?

You'll need to use two separate selectors with the same rule. CSS selectors don't really support alternation.

[class^='icon-'], [class*=' icon-'] {
/* ... */
}

div {  color: red;}
[class^='icon-'], [class*=' icon-'] { color: green;}
<div class='icon-something another-class'>should match</div><div class='another-class icon-something'>should match</div><div class='another-icon-class another-class'>should not match</div>

What is the regex of a CSS selector

So finally I've found a way better way to do it.

I use a LESS editor (eg: http://lesstester.com/)

and simply nest my whole css file:
.mySelector{ your css here }

How to include regex in the css selector in python selenium

This should do what you are looking for:

driver.find_element_by_css_selector('[id^="profileCard-"][id$="-EDUCATION-en-US"]').text

The ^ denotes what the id should begin with.

The $ denotes what the id should end with.



Related Topics



Leave a reply



Submit