Python跨模块变量调用问题求助:Selenium自动化项目共享模块变量
Hey there! Let's break down what's going on here and fix that error step by step.
Why You're Getting the NameError
In your Sample.py file, the variables a and b are local variables inside the Sum() method of the Integers class. Local variables only exist within the scope of the method they're defined in—once the method finishes running, they're gone. That's why when you try to print(a) in Test.py, Python has no idea what a you're talking about.
Fixing the Example Code
Here are a few common ways to make those variables accessible across modules, tailored to your use case:
1. Turn Variables into Instance Attributes
Make a and b properties of the class instance using self.—this lets any method in the class access them, and external code can access them via the instance:
# Sample.py class Integers(): def Sum(self): self.a = 6 # Use self. to make it an instance attribute self.b = 3 print(self.a + self.b) inst = Integers() inst.Sum()
Then in Test.py:
# Test.py import Sample print(Sample.inst.a) # Access via the instance you created in Sample.py print(Sample.inst.b)
2. Use Class Attributes (If Variables Don't Change Per Instance)
If a and b are fixed values for all instances of Integers, define them directly in the class (not inside a method):
# Sample.py class Integers(): a = 6 # Class attribute, shared across all instances b = 3 def Sum(self): print(self.a + self.b) inst = Integers() inst.Sum()
Then in Test.py:
# Test.py import Sample print(Sample.Integers.a) # Access via the class itself print(Sample.inst.b) # Or via an instance
3. Return Variables from the Method
If you only need to grab the values after running Sum(), have the method return them:
# Sample.py class Integers(): def Sum(self): a = 6 b = 3 print(a + b) return a, b # Return the variables as a tuple inst = Integers() # Store the returned values in variables at the module level inst_a, inst_b = inst.Sum()
Then in Test.py:
# Test.py import Sample print(Sample.inst_a) print(Sample.inst_b)
Applying This to Your Selenium Login Project
For your actual use case (sharing variables from Login.py with other test cases), here's a practical example:
Let's say your original Login.py looks like this (simplified):
# Login.py from selenium import webdriver class LoginPage(): def login(self): # Local variables—can't access these outside the method! driver = webdriver.Chrome() username = "test_user" password = "test_pass" # ... login steps ...
To share driver, username, etc., with other test scripts, convert them to instance attributes:
# Login.py from selenium import webdriver class LoginPage(): def __init__(self): # Initialize instance attributes here self.driver = webdriver.Chrome() self.username = "test_user" self.password = "test_pass" def login(self): # Use self. to access the attributes self.driver.get("https://your-login-url.com") self.driver.find_element("id", "username").send_keys(self.username) self.driver.find_element("id", "password").send_keys(self.password) self.driver.find_element("id", "submit").click() # Create an instance of LoginPage so other modules can use it login_instance = LoginPage() login_instance.login()
Now in your other test case script:
# TestCheckout.py import Login # Use the logged-in driver to navigate to another page Login.login_instance.driver.get("https://your-checkout-url.com") # Or access the username if needed print(Login.login_instance.username)
Key Takeaways
- Local variables (like
a=6inside a method) are trapped in their method's scope—never accessible outside. - To share variables across modules, use instance attributes (
self.xxx) for values that vary per instance, class attributes for shared fixed values, or return values from methods. - For Selenium projects, storing the WebDriver instance and test data as instance attributes of a page class is a standard practice that makes your test code cleaner and more maintainable.
内容的提问来源于stack exchange,提问作者Vikram Kumar

