1

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!

11
  • 6
    the indentation in your example needs to be fixed. Commented Apr 13, 2016 at 14:04
  • In what way? I don't see any error on IDE regarding identation Commented Apr 13, 2016 at 14:05
  • Python relies on indentation. The functions should be indented to be part of the class. Also, all functions in a class need self as the first parameter. To call another function in the class, do self. first. Commented Apr 13, 2016 at 14:08
  • 1
    If the functions are supposed to be in the class indent the whole thing. Indentation is part of the syntax in python, it's not for decoration. Commented Apr 13, 2016 at 14:08
  • whatever methods you intend to be part of the class, need to be tabbed in below the class, what's currently displayed shows the fn defs at the same indentation level as the class def Commented Apr 13, 2016 at 14:08

4 Answers 4

4

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()
Sign up to request clarification or add additional context in comments.

Comments

1

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:

  1. Indentation of methods is incorrect, class will be in level 0, methods will have level 1( 1 tab)..
  2. Class methods should have keyword self as a first parameters. For class fields use self.field_name
  3. When you call a class method use 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()

3 Comments

Ok, so now if I want to run runSelenium as my main function, how do I do that? runSelenium.selenium() doesn't seem to work and it asks for another parameter. Tried passing "self" to it but it fails
ok that will go into some kind of main function/area .. so first you need an object of type 'runSelenium' and then invoke 'selenium()' ... 'a=runSelenium()' , then 'a.selenium()'
Thanks a lot Mohammed! This helped me a lot!
1

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

2 Comments

Thanks for help! If I declare the webdriver in the init function how can I make sure it's still accessible to me in the other functions in the class?
It's a class attribute, so you can access it this way: self.driver
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.