请原谅noob问题。我不明白,有关这个“自我”的东西是:不明白“自我”错误
from selenium.common.exceptions import NoSuchElementException
def is_element_present(self, how, what):
try:
self.driver.find_element(by=how, value=what)
except NoSuchElementException:
return False
return True
#### navigate to the web page with a bunch of code here, then continue
text_present = self.assertTrue(self.is_element_present(By.LINK_TEXT, "Click Here"))
当我运行这一点,这是我得到的错误:
test_link = self.assertTrue(self.is_element_present(By.LINK_TEXT, "Click Here"))
NameError: name 'self' is not defined
我明白了“自我”所使用的约定函数中的第一个参数,但我不明白这个错误。我如何定义自我?
编辑1:我按照惯例修正了“自我”的用法(见下面的评论/回答)。但是,我仍然不知道我应该如何去满足Python解释器。
你打算“自我”是什么?你的代码根本不会创建任何对象。 – BrenBarn
'self'被用作实例方法而不是函数的第一个参数。两者之间存在相当大的差异。 – IanAuld
'self'不是习惯上用作函数的第一个参数。考虑花一些时间在网上提供的各种“学习python”教程之一(例如,[this](http://learnpythonthehardway.org/book/)或[this](http://www.codecademy.com)/EN /音轨/蟒蛇))。 – larsks