简体   繁体   中英

How to call a variable from one function to another in Python Selenium

I am writing automation code in Python Selenium I have a class which has many functions. I need to call a variable(name) from one function to another. How can I do this? Currently I have this code but its throwing me some errors.

test_1.py

class Properties(object):

     def __init__(self, driver):
     self.driver = driver
     
     def func1(self):
     name = self.driver.find_element_by_css_selector("#######").send_keys("******")

     def func2(self):
     self.driver.find_element_by_css_selector("######").send_keys(name)

Can I get some assistance and guidance here?

One option is to use self temporary value

class Properties(object):

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

    def func1(self):
        name = self.driver.find_element_by_css_selector("#######").send_keys("******")
        self.tmp_value = name

    def func2(self):
        self.driver.find_element_by_css_selector("######").send_keys(self.tmp_value)

The second is to return value from the first function to the second

class Properties(object):

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

    def func1(self):
        name = self.driver.find_element_by_css_selector("#######").send_keys("******")
        return name

    def func2(self):
        self.driver.find_element_by_css_selector("######").send_keys(self.func1())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM