Converting getCSS Count Code from java to python

J

Jon Clements

S

Stefan Behnel

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...count-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
 
S

SMERSH009

You also need somewhere:

    from selenium import selenium

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

You also need somewhere:

from selenium import selenium

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

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

Stefan Behnel

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

SMERSH009

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

SMERSH009

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top