Python - Passing argument of a function in a class
有谁能帮我使下面的代码工作吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import time from selenium import webdriver from selenium.webdriver.common.keys import Keys # class DataddoMobileTestFirefox(): # def setUp_and_login(self, browser): ''' Supported browsers are: firefox; chrome ''' global driver global email global password global login_btn # if browser == firefox: driver = webdriver.Firefox() elif browser == chrome: driver = webdriver.Chrome('/Library/Chromedriver/chromedriver') else: print('Browser not recognized, exiting...') exit() # # # Execute the module if __name__ == '__main__': DataddoMobileTestFirefox().setUp_and_login(firefox) |
当我执行模块时,会收到以下错误消息:
1 2 3 4 | Traceback (most recent call last): File"DataddoMobileFirefox.py", line 26, in DataddoMobileTestFirefox().setUp_and_login(firefox) NameError: name 'firefox' is not defined |
我相信我离目标很近,一些提示会对我有很大帮助。非常感谢。
您的模块中没有定义
您可以使用字符串:
1 | DataddoMobileTestFirefox().setUp_and_login('firefox') |
那么在你的
1 2 3 4 | if browser == 'firefox': driver = webdriver.Firefox() else browser == 'chrome': driver = webdriver.Chrome('/Library/Chromedriver/chromedriver') |
但最好是在你们班的
1 2 3 4 5 6 7 8 | from selenium import webdriver class DataddoMobileTestFirefox(): def __init__(self, driver): self.driver = webdriver.Firefox() def some_method(self): self.driver.do_something() |
现在,您可以从类中的其他方法访问驱动程序,而无需使用