1

My scenario

I only have one class with two methods. In the first method I store values in a variable. In the second method I try to call these variables

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

My error

AttributeError: 'UpdateTallysheet' object has no attribute 'value_1'

How can I use the variable value_1 in another method?

nick
  • 1,090
  • 1
  • 11
  • 24
Rafael C.
  • 2,245
  • 4
  • 29
  • 45

1 Answers1

6

You need to set it to an instance variable by using 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")

Then you can use it in another method by using self.value_1

Will
  • 4,299
  • 5
  • 32
  • 50
  • Worked Will. Thanks a lot. – Rafael C. Dec 21 '16 at 21:25
  • 1
    Glad to help! Also, an `__init__()` method may not be necessary, but I find it helps with keeping organized. See here: http://stackoverflow.com/questions/8609153/why-do-we-use-init-in-python-classes – Will Dec 21 '16 at 21:29
  • I'm having the same problem again, see my code here: https://gist.github.com/anonymous/61e2a757b451871867964966a36940d5 and I'm getting the error `AttributeError: 'CreateContractor' object has no attribute 'firstname'` Any idea? – Rafael C. Jan 24 '17 at 21:03
  • It's hard to say with no context, at first glace that code seems fine. If I were going to guess, I would say something to do with `fake.first_name()` or possibly with the order in which you are calling these methods. Open a new question and post not just the class, but the code that uses the class as well. – Will Jan 24 '17 at 21:18