Reputation: 1636
I'm a bit fresh with Python (doing usually C# stuff).. I am trying to use another function that was defined in the same class and for some reason I cannot access it.
class runSelenium:
def printTest():
print('This works')
def isElementPresent(locator):
try:
elem = driver.find_element_by_xpath(locator)
bRes = True
except AssertionError:
print('whatever')
else:
return False
def selenium():
driver = webdriver.Firefox()
driver.get("https://somesite.com/")
printTest()
isPresent = isElementPresent("//li[@class='someitem'][60]")
When trying to use printTest() and isElementPresent() I get: function not defined.. This is probably something ultra trivial I don't understand in Python.. Thanks for help!
Upvotes: 1
Views: 112
Reputation: 42528
Here are a few examples in python that should get you started:
class RunSelenium(object):
def printTest(self):
print('printTest 1!')
@staticmethod
def printTest2():
print('printTest 2!')
def printTest3():
print('printTest 3!')
# Call a method from an instantiated class
RunSelenium().printTest()
# Call a static method
RunSelenium.printTest2()
# Call a simple function
printTest3()
Upvotes: 4
Reputation: 4504
Here is another way to call a function in the same class:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
class runSelenium:
def __init__(self):
# define a class attribute
self.driver = None
def printTest(self):
print('This works')
def isElementPresent(self, locator):
try:
elem = self.driver.find_element_by_xpath(locator)
bRes = True
except NoSuchElementException:
print('whatever')
else:
return False
def selenium(self):
self.driver = webdriver.Firefox()
self.driver.get("https://somesite.com/")
self.printTest()
isPresent = self.isElementPresent("//li[@class='someitem'][60]")
if __name__ == '__main__':
# create an instance of class runSelenium
run = runSelenium()
# call function
run.selenium()
Upvotes: 1
Reputation: 656
In case you are using Python2.X
In your code every thing is interpreted sequentially not like a class, therefore, it cannot find the methods till they are defined. You have several mistakes here:
self
as a first parameters.
For class fields use self.field_name
self.method_name()
The code should be
class runSelenium:
def printTest(self):
print('This works')
def isElementPresent(self,locator):
try:
elem = driver.find_element_by_xpath(locator)
bRes = True
except AssertionError:
print('whatever')
else:
return False
def selenium(self):
driver = webdriver.Firefox()
driver.get("https://somesite.com/")
self.printTest()
isPresent = self.isElementPresent("//li[@class='someitem'][60]")
#Edit: To Run
a=runSelenium()
a.selenium()
Upvotes: 1
Reputation: 33
Indent your functions. As of now, they aren't part of your class.
class runSelenium:
def printTest():
print('This works')
def isElementPresent(locator):
try:
elem = driver.find_element_by_xpath(locator)
bRes = True
except AssertionError:
print('whatever')
else:
return False
def selenium():
driver = webdriver.Firefox()
driver.get("https://somesite.com/")
printTest()
isPresent = isElementPresent("//li[@class='someitem'][60]")
You aren't getting any IDE syntax errors because those functions don't have to belong to the class. But they must be indented under the class to be part of the class.
Upvotes: 0