RobVious
RobVious

Reputation: 12915

CSS Contains Selector with a NOT qualifier?

I'm using the following in some selenium code:

WaitForElement(By.CssSelector("#document-count:contains(<number greater than 0>)"));

Specifying the number greater than 0 is where I'm stuck. Is there any way to use only css to check and see if an element's innertext has something other than 0?

Upvotes: 0

Views: 1403

Answers (2)

Code Maverick
Code Maverick

Reputation: 20415

Per Chris Coyier at CSS Tricks:

Deprecated

:contains() - As far as I know, this is gone. The current CSS3 spec has removed it. I don't know the story, let me know if you do. At a glance, it looks ridiculously useful (being able to select objects based on the textual content they contain). It may be because of problems, or having content in selectors being undesirable. My preference would be to have it select by elements rather than text, like p:contains(img), but alas, no such luck.



That said, if you were to set the value properties, you may be able to use :not([value="0"]):

See jsFiddle demo



HTML

<div id="doc">
    <input type="text" value="0" />
    <br />
    <input type="text" value="1" />
    <br />
    <input type="text" value="2" />
</div>


CSS

#doc input[value="0"]
{
    background: red;
}

#doc input:not([value="0"])
{
    background: green;
}


Result

jsFiddle Result

Upvotes: 2

ddavison
ddavison

Reputation: 29032

:contains was deprecated in CSS3. Since WebDriver ties directly into the browser, it's unable to use that pseudo-class.

Is there any way to use only css to check and see if an element's innertext has something other than 0?

Unfortunately not. CSS really screwed Selenium users over with their deprecation of both :contains and :nth

As Arran said, you can use xpath, or - if you are willing to experiment with C# and CSS together (not just css as you state) then you can come up with something to loop x amount of times checking the text.

Upvotes: 2

Related Questions