访问同一类(Python)中的另一个函数时出现问题
我对Python(通常做C#东西)有点新鲜。。 我试图使用在同一个类中定义的另一个函数,但由于某些原因,我无法访问它访问同一类(Python)中的另一个函数时出现问题,python,Python,我对Python(通常做C#东西)有点新鲜。。 我试图使用在同一个类中定义的另一个函数,但由于某些原因,我无法访问它 class runSelenium: def printTest(): print('This works') def isElementPresent(locator): try: elem = driver.find_element_by_xpath(locator) bRes
class runSelenium:
def printTest():
print('This works')
def isElementPresent(locator):
try:
elem = driver.find_element_by_xpath(locator)
bRes = True
except AssertionError:
print('whatever')
else:
return False
def selenium():
driver = webdriver.Firefox()
driver.get("https://somesite.com/")
printTest()
isPresent = isElementPresent("//li[@class='someitem'][60]")
尝试使用printTest()和isElementPresent()时,我得到:未定义函数。。
这可能是我在Python中不理解的非常琐碎的东西。。
谢谢你的帮助 如果您正在使用Python2.X 在您的代码中,每件事情都是按顺序解释的,不像类,因此,在定义方法之前,它无法找到方法。这里有几个错误:
self
作为第一个参数。
对于类字段,请使用self.field\u name
self.method\u name()
class runSelenium:
def printTest(self):
print('This works')
def isElementPresent(self,locator):
try:
elem = driver.find_element_by_xpath(locator)
bRes = True
except AssertionError:
print('whatever')
else:
return False
def selenium(self):
driver = webdriver.Firefox()
driver.get("https://somesite.com/")
self.printTest()
isPresent = self.isElementPresent("//li[@class='someitem'][60]")
#Edit: To Run
a=runSelenium()
a.selenium()
如果您正在使用Python2.X 在您的代码中,每件事情都是按顺序解释的,不像类,因此,在定义方法之前,它无法找到方法。这里有几个错误:
self
作为第一个参数。
对于类字段,请使用self.field\u name
self.method\u name()
class runSelenium:
def printTest(self):
print('This works')
def isElementPresent(self,locator):
try:
elem = driver.find_element_by_xpath(locator)
bRes = True
except AssertionError:
print('whatever')
else:
return False
def selenium(self):
driver = webdriver.Firefox()
driver.get("https://somesite.com/")
self.printTest()
isPresent = self.isElementPresent("//li[@class='someitem'][60]")
#Edit: To Run
a=runSelenium()
a.selenium()
缩进你的函数。到目前为止,他们还不属于你们班
class runSelenium:
def printTest():
print('This works')
def isElementPresent(locator):
try:
elem = driver.find_element_by_xpath(locator)
bRes = True
except AssertionError:
print('whatever')
else:
return False
def selenium():
driver = webdriver.Firefox()
driver.get("https://somesite.com/")
printTest()
isPresent = isElementPresent("//li[@class='someitem'][60]")
您不会收到任何IDE语法错误,因为这些函数不必属于该类。但它们必须缩进到类下才能成为类的一部分 缩进函数。到目前为止,他们还不属于你们班
class runSelenium:
def printTest():
print('This works')
def isElementPresent(locator):
try:
elem = driver.find_element_by_xpath(locator)
bRes = True
except AssertionError:
print('whatever')
else:
return False
def selenium():
driver = webdriver.Firefox()
driver.get("https://somesite.com/")
printTest()
isPresent = isElementPresent("//li[@class='someitem'][60]")
您不会收到任何IDE语法错误,因为这些函数不必属于该类。但它们必须缩进到类下才能成为类的一部分 以下是一些python示例,可以帮助您入门:
class RunSelenium(object):
def printTest(self):
print('printTest 1!')
@staticmethod
def printTest2():
print('printTest 2!')
def printTest3():
print('printTest 3!')
# Call a method from an instantiated class
RunSelenium().printTest()
# Call a static method
RunSelenium.printTest2()
# Call a simple function
printTest3()
下面是一些python示例,可以帮助您入门:
class RunSelenium(object):
def printTest(self):
print('printTest 1!')
@staticmethod
def printTest2():
print('printTest 2!')
def printTest3():
print('printTest 3!')
# Call a method from an instantiated class
RunSelenium().printTest()
# Call a static method
RunSelenium.printTest2()
# Call a simple function
printTest3()
下面是在同一类中调用函数的另一种方法:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
class runSelenium:
def __init__(self):
# define a class attribute
self.driver = None
def printTest(self):
print('This works')
def isElementPresent(self, locator):
try:
elem = self.driver.find_element_by_xpath(locator)
bRes = True
except NoSuchElementException:
print('whatever')
else:
return False
def selenium(self):
self.driver = webdriver.Firefox()
self.driver.get("https://somesite.com/")
self.printTest()
isPresent = self.isElementPresent("//li[@class='someitem'][60]")
if __name__ == '__main__':
# create an instance of class runSelenium
run = runSelenium()
# call function
run.selenium()
下面是在同一类中调用函数的另一种方法:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
class runSelenium:
def __init__(self):
# define a class attribute
self.driver = None
def printTest(self):
print('This works')
def isElementPresent(self, locator):
try:
elem = self.driver.find_element_by_xpath(locator)
bRes = True
except NoSuchElementException:
print('whatever')
else:
return False
def selenium(self):
self.driver = webdriver.Firefox()
self.driver.get("https://somesite.com/")
self.printTest()
isPresent = self.isElementPresent("//li[@class='someitem'][60]")
if __name__ == '__main__':
# create an instance of class runSelenium
run = runSelenium()
# call function
run.selenium()
您的示例中的缩进需要修复。以什么方式修复?我在IDE上没有看到任何关于标识Python依赖缩进的错误。函数应该缩进以成为类的一部分。此外,类中的所有函数都需要
self
作为第一个参数。要调用类中的另一个函数,请先执行self.
操作。如果函数应该在类中,请缩进整个函数。缩进是python语法的一部分,不是为了修饰。无论您想要成为类的一部分的方法是什么,都需要在类的下面设置选项卡,当前显示的内容显示fn DEF与类DEF处于相同的缩进级别。在您的示例中,缩进需要固定。以何种方式?我在IDE上没有看到任何关于标识Python依赖缩进的错误。函数应该缩进以成为类的一部分。此外,类中的所有函数都需要self
作为第一个参数。要调用类中的另一个函数,请先执行self.
操作。如果函数应该在类中,请缩进整个函数。缩进是python语法的一部分,不是为了修饰。无论您想要成为类的一部分的方法是什么,都需要在类的下方设置选项卡,当前显示的是与类defOk处于相同缩进级别的fn def,所以现在如果我想运行runSelenium作为我的主函数,我该怎么做?runSelenium.selenium()似乎不起作用,它需要另一个参数。尝试将“self”传递给它,但失败了,它将进入某种主要功能/区域。。因此,首先需要一个“runSelenium”类型的对象,然后调用“selenium()”a=runSelenium(),然后是a.selenium(),非常感谢穆罕默德!这对我帮助很大!好的,现在如果我想运行runSelenium作为我的主函数,我该怎么做?runSelenium.selenium()似乎不起作用,它需要另一个参数。尝试将“self”传递给它,但失败了,它将进入某种主要功能/区域。。因此,首先需要一个“runSelenium”类型的对象,然后调用“selenium()”a=runSelenium(),然后是a.selenium(),非常感谢穆罕默德!这对我帮助很大!谢谢你的帮助!如果我在init函数中声明webdriver,如何确保我在类中的其他函数中仍然可以访问它?它是一个类属性,因此您可以通过以下方式访问它:self.driver
感谢帮助!如果我在init函数中声明webdriver,如何确保我在类中的其他函数中仍然可以访问它?它是一个类属性,因此您可以通过以下方式访问它:self.driver