【问题标题】:Calling a variable from another method in the same class从同一类中的另一个方法调用变量
【发布时间】:2019-08-21 01:02:28
【问题描述】:

我的场景

我只有一个类有两种方法。在第一种方法中,我将值存储在变量中。在第二种方法中,我尝试调用这些变量

class UpdateTallysheet(Page):

    def confirme_status_capture_exp_value(self):

        self.select_dropdown_value(EventsLocators.STATUS, "8")
        value_1 = self.find_element(EventsLocators.EXAM_EXP_VALUE).get_attribute("value")
        value_2 = self.find_element(EventsLocators.PANO_EXP_VALUE).get_attribute("value")
        value_3 = self.find_element(EventsLocators.TREATMENT_EXP_VALUE).get_attribute("value")

        self.find_element(EventsLocators.SAVE_BUTTON).click()
        WebDriverWait(self.driver, AUTOCOMPLETE_TIMEOUT).until(
            EC.text_to_be_present_in_element((By.CLASS_NAME, "success"), 'Event updated successfully'))
        self.find_element(EventsLocators.TALLYSHEET_LINK).click()

    def fill_date(self):

        self.select_current_date(EventsLocators.DATE_RECEIVED, EventsLocators.CURRENT_DATE)
        self.select_current_date(EventsLocators.DATE_COMPLETED, EventsLocators.CURRENT_DATE)
        print self.value_1

我的错误

AttributeError: 'UpdateTallysheet' 对象没有属性 'value_1'

如何在其他方法中使用变量value_1

【问题讨论】:

    标签: python python-2.7 oop


    【解决方案1】:

    您需要使用self将其设置为实例变量:

    class UpdateTallysheet(Page):
        def confirme_status_capture_exp_value(self):
            self.select_dropdown_value(EventsLocators.STATUS, "8")
            self.value_1 = self.find_element(EventsLocators.EXAM_EXP_VALUE).get_attribute("value")
            self.value_2 = self.find_element(EventsLocators.PANO_EXP_VALUE).get_attribute("value")
            self.value_3 = self.find_element(EventsLocators.TREATMENT_EXP_VALUE).get_attribute("value")
    

    然后你可以通过self.value_1在其他方法中使用它

    【讨论】:

    • 工作遗嘱。非常感谢。
    • 很高兴为您提供帮助!此外,__init__() 方法可能不是必需的,但我发现它有助于保持井井有条。见这里:stackoverflow.com/questions/8609153/…
    • 我又遇到了同样的问题,在这里查看我的代码:gist.github.com/anonymous/61e2a757b451871867964966a36940d5 我收到错误AttributeError: 'CreateContractor' object has no attribute 'firstname' 任何想法?
    • 在没有上下文的情况下很难说,乍一看,代码似乎很好。如果我要猜测,我会说与fake.first_name() 或可能与您调用这些方法的顺序有关。打开一个新问题,不仅发布课程,还发布使用该课程的代码。
    猜你喜欢
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 2013-10-08
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多