FAQ
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/

Thanks for the help

Search Discussions

  • Jon Clements at Feb 2, 2011 at 12:52 pm

    On Feb 1, 4:23?am, SMERSH009 wrote:
    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-h...

    Thanks for the help
    Maybe?

    http://codespeak.net/lxml/dev/cssselect.html

    Jon.
  • Stefan Behnel at Feb 2, 2011 at 1:14 pm

    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
  • Duncan Booth at Feb 2, 2011 at 3:03 pm

    Stefan Behnel wrote:

    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.
    You also need somewhere:

    from selenium import selenium

    and in Python the method that is needed is selenium.get_eval(...)
  • SMERSH009 at Feb 17, 2011 at 9:46 pm

    On Feb 2, 7:03?am, Duncan Booth wrote:
    Stefan Behnel wrote:
    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.
    You also need somewhere:

    ? ? from selenium import selenium

    and in Python the method that is needed is selenium.get_eval(...)

    --
    Duncan Boothhttp://kupuguy.blogspot.com
    On Feb 2, 7:03 am, Duncan Booth wrote:
    Stefan Behnel wrote:
    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.
    You also need somewhere:

    from selenium import selenium

    and in Python the method that is needed is selenium.get_eval(...)

    --
    Duncan Boothhttp://kupuguy.blogspot.com
    Hi All,
    Thanks a lot for the help. I feel like I've made some progress, yet I
    am still stuck with the following error when I try to
    print self.count_css_matches('css=[id="listGuests"]')
    I've also included the Selenium code below. Any further help would be
    appreciated.

    Traceback (most recent call last):
    File "D:\Temp\1TestingApps\Selenium\Scripts\SevPractice.py", line
    27, in test_untitled
    print self.count_css_matches('css=[id="listGuests"]')
    File "D:\Temp\1TestingApps\Selenium\Scripts\SevPractice.py", line
    17, in count_css_matches
    return int(selenium.get_eval(self, java_script_code))
    TypeError: unbound method get_eval() must be called with selenium
    instance as first argument (got Untitled instance instead)

    ----------------------------------------------------------------------


    from selenium import selenium
    import unittest, time
    from datetime import datetime

    class Untitled(unittest.TestCase):
    def count_css_matches(self, css_locator):
    java_script_code = '''
    var cssMatches = eval_css("%s", window.document);
    cssMatches.length;''' % css_locator
    return int(selenium.get_eval(self, java_script_code))
    #return int(selenium.getEval(java_script_code))

    def setUp(self):
    self.verificationErrors = []
    self.selenium = selenium("localhost", 4445, "*chrome", "http://
    www.guestlistnation.com/")
    self.selenium.start()



    def test_untitled(self):
    sel = self.selenium
    sel.window_maximize()
    sel.open("/Events.aspx?Location=SAN FRANCISCO")

    sel.click("css=[id='EventDates_ctl00_NestedEvents_ctl01_btnDetails']")
    sel.wait_for_page_to_load("30000")

    print self.count_css_matches('css=[id="listGuests"]')

    if __name__ == "__main__":
    unittest.main()
  • Stefan Behnel at Feb 18, 2011 at 5:51 am

    SMERSH009, 17.02.2011 22:46:
    am still stuck with the following error when I try to
    print self.count_css_matches('css=[id="listGuests"]')
    I've also included the Selenium code below. Any further help would be
    appreciated.

    Traceback (most recent call last):
    File "D:\Temp\1TestingApps\Selenium\Scripts\SevPractice.py", line
    27, in test_untitled
    print self.count_css_matches('css=[id="listGuests"]')
    File "D:\Temp\1TestingApps\Selenium\Scripts\SevPractice.py", line
    17, in count_css_matches
    return int(selenium.get_eval(self, java_script_code))
    TypeError: unbound method get_eval() must be called with selenium
    instance as first argument (got Untitled instance instead)

    ----------------------------------------------------------------------


    from selenium import selenium
    import unittest, time
    from datetime import datetime

    class Untitled(unittest.TestCase):
    def count_css_matches(self, css_locator):
    java_script_code = '''
    var cssMatches = eval_css("%s", window.document);
    cssMatches.length;''' % css_locator
    return int(selenium.get_eval(self, java_script_code))
    #return int(selenium.getEval(java_script_code))
    You want to use "self.selenium" here, not "selenium".

    Stefan

    def setUp(self):
    self.verificationErrors = []
    self.selenium = selenium("localhost", 4445, "*chrome", "http://
    www.guestlistnation.com/")
    self.selenium.start()



    def test_untitled(self):
    sel = self.selenium
    sel.window_maximize()
    sel.open("/Events.aspx?Location=SAN FRANCISCO")

    sel.click("css=[id='EventDates_ctl00_NestedEvents_ctl01_btnDetails']")
    sel.wait_for_page_to_load("30000")

    print self.count_css_matches('css=[id="listGuests"]')

    if __name__ == "__main__":
    unittest.main()
  • SMERSH009 at Feb 18, 2011 at 6:25 am

    On Feb 17, 9:51?pm, Stefan Behnel wrote:
    SMERSH009, 17.02.2011 22:46:


    am still stuck with the following error when I try to
    print self.count_css_matches('css=[id="listGuests"]')
    I've also included the Selenium code below. Any further help would be
    appreciated.
    Traceback (most recent call last):
    ? ?File "D:\Temp\1TestingApps\Selenium\Scripts\SevPractice.py", line
    27, in test_untitled
    ? ? ?print self.count_css_matches('css=[id="listGuests"]')
    ? ?File "D:\Temp\1TestingApps\Selenium\Scripts\SevPractice.py", line
    17, in count_css_matches
    ? ? ?return int(selenium.get_eval(self, java_script_code))
    TypeError: unbound method get_eval() must be called with selenium
    instance as first argument (got Untitled instance instead)
    ----------------------------------------------------------------------
    from selenium import selenium
    import unittest, time
    from datetime import datetime
    class Untitled(unittest.TestCase):
    ? ? ?def count_css_matches(self, css_locator):
    ? ? ? ? ?java_script_code = '''
    ? ? ? ? ? ? ?var cssMatches = eval_css("%s", window.document);
    ? ? ? ? ? ? ?cssMatches.length;''' % css_locator
    ? ? ? ? ?return int(selenium.get_eval(self, java_script_code))
    ? ? ? ? ?#return int(selenium.getEval(java_script_code))
    You want to use "self.selenium" here, not "selenium".

    Stefan
    ? ? ?def setUp(self):
    ? ? ? ? ?self.verificationErrors = []
    ? ? ? ? ?self.selenium = selenium("localhost", 4445, "*chrome", "http://
    www.guestlistnation.com/")
    ? ? ? ? ?self.selenium.start()
    ? ? ?def test_untitled(self):
    ? ? ? ? ?sel = self.selenium
    ? ? ? ? ?sel.window_maximize()
    ? ? ? ? ?sel.open("/Events.aspx?Location=SAN FRANCISCO")
    sel.click("css=[id='EventDates_ctl00_NestedEvents_ctl01_btnDetails']")
    ? ? ? ? ?sel.wait_for_page_to_load("30000")
    ? ? ? ? ?print self.count_css_matches('css=[id="listGuests"]')
    if __name__ == "__main__":
    ? ? ?unittest.main()
    Wow-- you guys rock --big time! :))
    Here is the final code with a working examples:


    from selenium import selenium
    import unittest, time
    from datetime import datetime

    class Untitled(unittest.TestCase):
    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)


    def setUp(self):
    self.verificationErrors = []
    self.selenium = selenium("localhost", 4445, "*chrome", "http://
    www.guestlistnation.com/")
    self.selenium.start()

    def test_untitled(self):
    sel = self.selenium
    sel.window_maximize()
    sel.open("/Events.aspx?Location=SAN FRANCISCO")


    sel.click("css=[id='EventDates_ctl00_NestedEvents_ctl01_btnDetails']")
    sel.wait_for_page_to_load("30000")

    print self.count_css_matches("[id='listGuests'] > option") #
    prints number of options in dropdown with 'id=listGuests'
    print self.count_css_matches("*") #prints all on page
    if __name__ == "__main__":
    unittest.main()
  • Stefan Behnel at Feb 18, 2011 at 6:42 am

    SMERSH009, 18.02.2011 07:25:
    On Feb 17, 9:51 pm, Stefan Behnel wrote:
    SMERSH009, 17.02.2011 22:46:
    class Untitled(unittest.TestCase):
    def count_css_matches(self, css_locator):
    java_script_code = '''
    var cssMatches = eval_css("%s", window.document);
    cssMatches.length;''' % css_locator
    return int(selenium.get_eval(self, java_script_code))
    #return int(selenium.getEval(java_script_code))
    You want to use "self.selenium" here, not "selenium".
    Wow-- you guys rock --big time! :))
    It's just Linus' Law.

    http://en.wikipedia.org/wiki/Linus%27_Law

    Stefan
  • SMERSH009 at Feb 18, 2011 at 7:09 am

    On Feb 17, 10:25?pm, SMERSH009 wrote:
    On Feb 17, 9:51?pm, Stefan Behnel wrote:


    SMERSH009, 17.02.2011 22:46:
    am still stuck with the following error when I try to
    print self.count_css_matches('css=[id="listGuests"]')
    I've also included the Selenium code below. Any further help would be
    appreciated.
    Traceback (most recent call last):
    ? ?File "D:\Temp\1TestingApps\Selenium\Scripts\SevPractice.py", line
    27, in test_untitled
    ? ? ?print self.count_css_matches('css=[id="listGuests"]')
    ? ?File "D:\Temp\1TestingApps\Selenium\Scripts\SevPractice.py", line
    17, in count_css_matches
    ? ? ?return int(selenium.get_eval(self, java_script_code))
    TypeError: unbound method get_eval() must be called with selenium
    instance as first argument (got Untitled instance instead)
    ----------------------------------------------------------------------
    from selenium import selenium
    import unittest, time
    from datetime import datetime
    class Untitled(unittest.TestCase):
    ? ? ?def count_css_matches(self, css_locator):
    ? ? ? ? ?java_script_code = '''
    ? ? ? ? ? ? ?var cssMatches = eval_css("%s", window.document);
    ? ? ? ? ? ? ?cssMatches.length;''' % css_locator
    ? ? ? ? ?return int(selenium.get_eval(self, java_script_code))
    ? ? ? ? ?#return int(selenium.getEval(java_script_code))
    You want to use "self.selenium" here, not "selenium".
    Stefan
    ? ? ?def setUp(self):
    ? ? ? ? ?self.verificationErrors = []
    ? ? ? ? ?self.selenium = selenium("localhost", 4445, "*chrome", "http://
    www.guestlistnation.com/")
    ? ? ? ? ?self.selenium.start()
    ? ? ?def test_untitled(self):
    ? ? ? ? ?sel = self.selenium
    ? ? ? ? ?sel.window_maximize()
    ? ? ? ? ?sel.open("/Events.aspx?Location=SAN FRANCISCO")
    sel.click("css=[id='EventDates_ctl00_NestedEvents_ctl01_btnDetails']")
    ? ? ? ? ?sel.wait_for_page_to_load("30000")
    ? ? ? ? ?print self.count_css_matches('css=[id="listGuests"]')
    if __name__ == "__main__":
    ? ? ?unittest.main()
    Wow-- you guys rock --big time! :))
    Here is the final code with a working examples:

    from selenium import selenium
    import unittest, time
    from datetime import datetime

    class Untitled(unittest.TestCase):
    ? ? 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)

    ? ? def setUp(self):
    ? ? ? ? self.verificationErrors = []
    ? ? ? ? self.selenium = selenium("localhost", 4445, "*chrome", "http://www.guestlistnation.com/")
    ? ? ? ? self.selenium.start()

    ? ? def test_untitled(self):
    ? ? ? ? sel = self.selenium
    ? ? ? ? sel.window_maximize()
    ? ? ? ? sel.open("/Events.aspx?Location=SAN FRANCISCO")

    sel.click("css=[id='EventDates_ctl00_NestedEvents_ctl01_btnDetails']")
    ? ? ? ? sel.wait_for_page_to_load("30000")

    ? ? ? ? print self.count_css_matches("[id='listGuests'] > option") #
    prints number of options in dropdown with 'id=listGuests'
    ? ? ? ? print self.count_css_matches("*") #prints all on page
    if __name__ == "__main__":
    ? ? unittest.main()
    oops, forgot to put back the int conversion!

    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))

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 1, '11 at 4:23a
activeFeb 18, '11 at 7:09a
posts9
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase