Converting getCSS Count Code from java to python

Stefan Behnel stefan_ml at behnel.de
Wed Feb 2 08:14:15 EST 2011


SMERSH009, 01.02.2011 05:23:
> Hi, I'd love some help converting this code to the python equivalent:
>
> 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)));
> }
>
> http://www.eviltester.com/index.php/2010/03/13/a-simple-getcsscount-helper-method-for-use-with-selenium-rc/

You are using Selenium RC here. I have no idea if there is a Python API to 
it or what that API looks like. The rest is just trivial code that you can 
map 1:1 to Python:

     def count_css_matches(css_locator):
         java_script_code = '''
             var cssMatches = eval_css("%s", window.document);
             cssMatches.length;''' % css_locator
         return int(selenium.getEval(java_script_code))

Although I'd simplify the JavaScript code somewhat to make it a plain 
expression.

If that's not what you want, please be more precise, or look at the CSS 
selectors in lxml that Jon Clemens pointed you to.

Stefan




More information about the Python-list mailing list