简体   繁体   中英

Python, selenium webdriver - I need base class method to return type of its child class. How to achieve it?

I am quite new to Python (and stackoverflow) and I need some help. I have 2 classes as below

class baseClass(object):    
    def __init__(self):
        self.attribute = None        
    def baseMethod(self):
        return 'xxx' + str(self.attribute)    
class classA(baseClass):
    field = "test"    
    def __init__(self):
        self.attribute = "attribute value"

And I would like to do something like below:

objectA = classA()
afterBaseMethod = objectA.baseMethod()
afterBaseMethod.field

But I'm getting error message AttributeError: 'str' object has no attribute 'field'

In fact I would like for baseMethod() to return type objectA (so my child class type). How can I change my class definitions to achieve that?


Sorry Guys, I was not precise enough what I want to do. In fact I would like to implement page object in python for webdriver testing. My code below.

from selenium import webdriver

########## classes definitions
class BasePageObject:

    def __init__(self, driver):
        self.locator = None
        self.driver = driver
    def getByLocator(self):
        return self.driver.find_element_by_css_selector(self.locator)

class MainPage(BasePageObject):
    def __init__(self, driver):
        self.driver = driver 
    def getHeader(self):
        # I expect here to receive HeaderPageObject, not WebElement (as returned by getByLocator())
        return HeaderPageObject(self.driver).getByLocator() 

class HeaderPageObject(BasePageObject):
    def __init__(self, driver):
        self.driver = driver
        self.locator = '#header'

    def doSomethingWithHeader(self):
        pass
###########################
driver = webdriver.Firefox()
driver.get("https://www.linkedin.com")
mainPage = MainPage(driver)
header = mainPage.getHeader()
header.doSomethingWithHeader()

and when I run it I'm getting header.doSomethingWithHeader() AttributeError: 'WebElement' object has no attribute 'doSomethingWithHeader'

Your code as published simply defines the two classes, which means that you haven't even told us where this reported error occurs or what you did to induce it. But Python's class mechanism is well up to this task.

Objects know what type they are, so even when a method is inherited from a superclass it runs on the subclass instance ( ie self will be an instance of the subclass). Here's some code to demonstrate - two classes, each of which use the base class to report their type:

class baseClass(object):    
    def baseMethod(self):
        return type(self)    

class classA(baseClass):
    pass

b = baseClass()
print b.baseMethod()

a = classA()
print a.baseMethod()

which outputs

<class '__main__.baseClass'>
<class '__main__.classA'>

You can see that a classA object prints its type out correctly, so it knows what type of object made the call. I hope this tells you what you need to know.

If you want it to return the class from which it was called, you can do:

def baseMethod(self):
    return self.__class__()

However, it's not clear from your post what else you want it to do. Your post just shows it returning a string that will be the same for every instance of your class. It's not clear what the purpose of baseMethod is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM