我正在尝试使用Selenium / Python
检查HTML页面上是否存在元素这是我的功能:
class runSelenium(object):
def __init__(self):
# define a class attribute
self.driver = webdriver.Firefox()
def isElementPresent(self, locator):
try:
self.driver.find_element_by_xpath(locator)
except NoSuchElementException:
print ('No such thing')
return False
return True
def selenium(self):
self.driver.get("https://somepage.com")
isElement = self.isElementPresent("//li[@class='item'][6]")
isElement1 = str(isElement)
if __name__ == '__main__':
run = runSelenium()
run.selenium()
我试图用布尔变量选择结果:
isElement = self.isElementPresent("//li[@class='item'][6]")
但由于某种原因,我没有看到它接受任何布尔值,True或False。 我在这里错过了什么? 在此先感谢!!
答案 0 :(得分:2)
您需要取消缩进最后一个代码块:
class runSelenium(object):
def __init__(self):
# define a class attribute
self.driver = webdriver.Firefox()
def isElementPresent(self, locator):
try:
self.driver.find_element_by_xpath(locator)
except NoSuchElementException:
print ('No such thing')
return False
return True
def selenium(self):
self.driver.get("https://somepage.com")
isElement = self.isElementPresent("//li[@class='item'][6]")
isElement1 = str(isElement)
if __name__ == '__main__':
run = runSelenium()
run.selenium()
答案 1 :(得分:-1)
所以我重新启动了IDE(Visual Studio 2013),它现在运行正常..代码是正确的100%