A simple getCSSCount for use with Selenium-RC
We know that XPath runs slowly in IE, but XPath has the getXPathCount method. And CSS runs quickly but Selenium doesn’t have a corresponding getCSSCount method.
I looked around for a simple way of getting count from a CSS selector.
I found this blog post by Aditya Ivaturi, but since I like to keep my Selenium setup and tests pretty simple, I wanted a much lower maintenance way of implementing the getCSSCount functionality.
So rather than amending the core in the jar file as Aditya did, or using user-extensions, I took the key JavaScript functionality presented in Aditya’s blog post and ran it with a get_eval.
Thus the following helper method:
private int getCSSCount(String aCSSLocator){
String jsScript = "var cssMatches = eval_css(\"%s\", window.document);cssMatches.length;";
return Integer.parseInt(selenium.getEval(String.format(jsScript, aCSSLocator)));
}
Which you could use:
assertEquals(6, getCSSCount("*"));
assertEquals(1,getCSSCount("p[id='para1']"));
This satisfied the itch I needed to scratch and it seems pretty low maintenance between Selenium versions.


Awesome! Your posts on CSS selectors are very timely for me.
So the C# version is:
private int getCSSCount(String aCSSLocator)
{
String jsScript = “var cssMatches = eval_css(\”" + aCSSLocator + “\”, window.document);cssMatches.length;”;
return int.Parse(browser.GetEval(jsScript));
}
Where I call the following C# to return the number of options in a element named “realm”:
int numOptions = getCSSCount(“select[name='realm'] option”);
or
int numOptions = getCSSCount(“select[name='realm']>option”);
Thanks for converting it into C#, I hope that can help someone else too.
hi, thank you for this. Can you please also explain how to add “Get CSS count” into the userextensions.js file so that it can be its own function within selenium?
best,
S
I don’t use the IDE or userextensions.js so everything I do is based around simple Java. I’ve approved your comment in case someone else sees it and can help.
But looking at the official docs http://seleniumhq.org/docs/08_user_extensions.html
It looks like it might be as simple as adding
Selenium.prototype.getCSSCount = function(locator, text) {var cssMatches = eval_css(locator, window.document);
return cssMatches.length;
};
or possibly
Selenium.prototype.getCSSCount = function(locator, text) {var cssMatches = eval_css(locator, this.browserbot.getDocument());
return cssMatches.length;
};
But I haven’t tried either of those code snippets so I’m coding blind based on the docs and based on Aditya’s blog post.
Thank you for the attempt to help, but I couldn’t get that extension to work.
However–there is some good news. the kind folks at the google python user group have helped convert your original function into python. So now if anyone wants to use it, they would just place this code within their unittest class.
def count_css_matches(self, css_locator):
java_script_code = ”’
var cssMatches = eval_css(“%s”, window.document);
cssMatches.length;”’ % css_locator
return self.selenium.get_eval(java_script_code)
print self.count_css_matches(“[id='listGuests'] > option”) # where option is the name of the options elements in dropdown with id=’listGuests’
Suweeeeet!!! :)
oops sorry forgot to put back the int conversion
return int(self.selenium.get_eval(java_script_code))
So the code is :
def count_css_matches(self, css_locator):
java_script_code = ”’
var cssMatches = eval_css(”%s”, window.document);
cssMatches.length;”’ % css_locator
return int(self.selenium.get_eval(java_script_code))
print self.count_css_matches(”[id=’listGuests’] > option”) # where option is the name of the options elements in dropdown with id=’listGuests’