0

I am using inheritance to access base class elements. I have defined driver object in environment class and inherited in base class. In base class I am trying to access this object.

However I am getting an error Environment has no object driver. How do I access this element?

class Environment(object):
  def __init__(self):
        driver = "webdriver.Chrome(D:\BrowserDriver\ChromeDriver\chromedriver.exe)"
        print self.driver


class base(Environment):
  def __init__(self):
    drive = self.driver
  def test_Home_Page(self):

# Screenshots relative paths
        ss_path = "/Test_MercuryTours_HomePage/"

# Using the driver instances created in EnvironmentSetup
        drive = self.driver
        print drive
env=Environment()
print env.setUp()
b=base()
print b.drive

2 Answers 2

1
class Environment(object):
  def __init__(self):
     self.driver = "webdriver.Chrome(D:\BrowserDriver\ChromeDriver\chromedriver.exe)"
     print self.driver


class base(Environment):
  def __init__(self):
     Environment.__init__(self)
     self.drive = self.driver


b=base()
print b.drive
Sign up to request clarification or add additional context in comments.

1 Comment

so just inheriting is not enough?
1

Add self to the variable driver, in the base class.
def __init__(self): self.driver = "webdriver.C..."
ps: similarly, to access drive, you need to change it to self.drive .
In the base class, try this.
def __init__(self): Environment.__init__(self) self.driver = "webdriver.C..." .
Find out more about the Super keyword used for inheritance.

3 Comments

I did and the result is the same.I get an error saying driver(referring to the base class) is AttributeError: 'base' object has no attribute 'driver'
In the base class, try this. def __init__(self): Environment.__init__(self) . self.driver = "webdriver.C..."
stackoverflow.com/questions/576169/… . This would provide a better understanding of super keyword used in inheritance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.