我对编写类不是很有经验,并且对他们的行为有些困惑。
test.py
class Profile:
def __init__(self):
self.browser = webdriver.Chrome()
def browser_object(self):
return webdriver.Chrome()
从终端:
from test import Profile
# I thought this would initialize webdriver.Chrome() and a browser window would pop up
x = Profile.browser_object()
# trying to access the webdriver so I can do something like
x.get(url) #from the terminal
没有弹出浏览器窗口。当我跑步时,x
我看到它是一个<function Profile.browser_object ...>
我究竟做错了什么?
尝试更改您的其他脚本以实际创建您在test.py
.
from test import Profile
profile_instance = Profile() # make an instance
browser_I_made = profile_instance.browser_object() # you probably wouldn't do this, but it's possible
我认为你不明白一个类就像一个工厂,你从流水线上得到那个对象的“实例”,就像 profile_instance = Profile()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句