0

I have class method generated_username which is generate string and return that string, and I need to use this output in another method new_full_list, but I get run the whole method not just its output Here is my code:

class Users():

def generated_username(self, driver):
    username = driver.find_element_by_css_selector("[id=systemUser_employeeName_empName]").get_attribute('value')
    username = username.replace(' ', '').lower()
    username = username + ''.join(random.choice(string.digits) for i in range(3))
    return username

def new_full_list(self,driver):
    l = driver.find_elements_by_xpath("//*[@class='odd' or @class='even']/td[2]/a")
    l = [x.text for x in l]
    l.append(self.generated_username(driver))
2
  • What is the current output you are receiving, and what do you want it to be instead? Your question is not too clear. Commented Feb 28, 2018 at 4:33
  • @Windmill in line l.append(self.generated_username(driver)) i want to append string which is created in generated_username but instead of this generated_username runs again (try to find element driver.find_element_by_css_selector("[id=systemUser_employeeName_empName]").get_attribute('value')) and then generate string and then return, but in that time i already on different page, so driver can not find this element driver.find_element_by_css_selector("[id=systemUser_employeeName_empName]").get_attribute('value') Commented Feb 28, 2018 at 23:26

1 Answer 1

1

If I understand correctly, you would like generated_username to be called on a particular page, and generate a string. Later, on a different page, you want to use that earlier generated string in new_full_list.

To do this, you could create a class variable to hold this string.

class Users():
    user_string = ""

Then, when on the correct page (I'm not sure what the rest of your code looks like/how you control the flow/when functions are called), you call generated_username(driver). We will modify generated_username to set the class variable we created earlier.

def generated_username(self, driver):
    ...
    <s>return username</s>
    this.user_string = username

When you later call new_full_list(driver) on a different page, you want it to use this previous value. We can do that as follows:

def new_full_list(self,driver):
    l = driver.find_elements_by_xpath("//*[@class='odd' or @class='even']/td[2]/a")
    l = [x.text for x in l]
    l.append(self.user_string)

Right now, you are calling generated_username inside new_full_list, which means the entire function runs, including the find_element etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.