检查页面元素是否存在

 1 # -*- coding:utf-8 -*-
 2 
 3 """
 4 检查元素是否存在
 5 isElementPresent():检查页面的元素上是否存在
 6 """
 7 
 8 from selenium import webdriver
 9 from selenium.common.exceptions import NoSuchElementException
10 from selenium.webdriver.common.by import By
11 
12 class IsElement(object):
13     def __init__(self):
14         self.driver = webdriver.Firefox()
15         
16     def isElementPresent(self, how, what):
17         try:
18             self.driver.find_element(how, what)
19         except NoSuchElementException as e:
20             return False        
21         return True
22     
23     def testisElementPresent(self):
24         self.driver.get('http://www.baidu.com')
25         self.driver.find_element_by_id('kw').send_keys('selenium')
26         
27         if self.isElementPresent(By.ID, 'su'):
28             self.driver.find_element(By.ID, 'su').click()
29         else:
30             raise '元素不存在'
31         
32 if __name__ == '__main__':
33     test = IsElement()
34     test.testisElementPresent()
35             
36         

 

posted @ 2013-07-06 21:19  Roger|  阅读(469)  评论(0编辑  收藏  举报