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
Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40
leo
  • 69
  • 1
  • 8

2 Answers2

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.

  • 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' – leo Mar 15 '18 at 05:41
  • In the base class, try this. `def __init__(self): Environment.__init__(self) . self.driver = "webdriver.C..." ` – Sagar Ruchandani Mar 15 '18 at 05:47
  • https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods . This would provide a better understanding of super keyword used in inheritance. – Sagar Ruchandani Mar 15 '18 at 05:49
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
jonhid
  • 2,075
  • 1
  • 11
  • 18