【问题标题】:Newbie here! When using a method of one class inside a method of another class, do I have to pass the self argument as well新手来了!在另一个类的方法中使用一个类的方法时,我是否也必须传递 self 参数
【发布时间】:2020-08-19 17:31:53
【问题描述】:

我正在尝试编写测试脚本。这是我的代码。

class baseclass():

   def __init__(self, driver):
       self.driver=driver

   def click_on(self, what):
       self.driver.find_element(*what).click()

class initialpage(baseclass):

    def __init__(self, driver):
        super().__init__(driver)

    def click_mailbtn(self):
        baseclass.click_on(locators.mail_icon_xpath)

当我在 pytest 中运行测试时,它给了我一个 error

TypeError: click_on() missing 1 required positional argument: 'what'

当我在下面的代码中传递 self 参数时,它工作正常

def click_mailbtn(self):
        baseclass.click_on(**self**, locators.mail_icon_xpath) 

在这种情况下是否有必要传递 self 参数?还是我在这里做错了什么?提前致谢!

【问题讨论】:

    标签: python selenium-webdriver pytest


    【解决方案1】:

    由于initialpage 继承自baseclass,您通常会像这样调用click_on

    class Base(object):
        def __init__(self, driver):
            self.driver = driver        
    
        def click_on(self, what):
            self.driver.find_element(*what).click()
    
    class InitialPage(Base):
    
        def click_mail_btn(self):
            self.click_on(locators.mail_icon_xpath)
    

    我建议阅读the Python documentation on inheritance,了解更多关于这里实际发生的事情以及为什么这样调用继承方法的详细信息。

    【讨论】:

      猜你喜欢
      • 2021-03-24
      • 2010-09-23
      • 2018-04-10
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      相关资源
      最近更新 更多