如何使用继承

时间:2018-03-15 05:30:41

标签: python inheritance

我正在使用继承来访问基类元素。我在环境类中定义了驱动程序对象,并在基类中继承。在基类中,我试图访问此对象。

但是我收到错误环境没有对象驱动程序。如何访问此元素?

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 个答案:

答案 0 :(得分:1)

将self添加到基类中的变量驱动程序 def __init__(self): self.driver = "webdriver.C..."
ps:同样,要访问驱动器,您需要将其更改为self.drive 在基类中,试试这个 def __init__(self): Environment.__init__(self) self.driver = "webdriver.C..."
了解有关用于继承的超级关键字的更多信息。

答案 1 :(得分: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