基础类封装-查找页面元素类库封装
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 # @Time : 2019/10/14 23:37 4 # @Author : Tang Yiwei 5 # @Email : 892398433@qq.com 6 # @File : ObjectsMap.py 7 # @Software: PyCharm 8 9 import traceback 10 from selenium.webdriver.support.ui import WebDriverWait 11 from selenium.webdriver.common.by import By 12 from selenium.common.exceptions import NoSuchElementException,TimeoutException 13 14 def findElement(driver, selector, second=30): 15 """获取单个页面元素对象""" 16 wait = WebDriverWait(driver, second, 0.1) 17 try: 18 if '=>' not in selector: 19 element = wait.until(lambda x: x.find_element(by=By.ID, value=selector)) 20 return element 21 locationType = selector.split('=>')[0] 22 locatorExpression = selector.split('=>')[1] 23 if locationType.lower() in ['i', 'id']: 24 element = wait.until(lambda x: x.find_element(by=By.ID, value=locatorExpression)) 25 elif locationType.lower() in ['n', 'name']: 26 element = wait.until(lambda x: x.find_element(by=By.NAME, value=locatorExpression)) 27 elif locationType.lower() in ['c', 'class']: 28 element = wait.until(lambda x: x.find_element(by=By.CLASS_NAME, value=locatorExpression)) 29 elif locationType.lower() in ['l', 'link']: 30 element = wait.until(lambda x: x.find_element(by=By.LINK_TEXT, value=locatorExpression)) 31 elif locationType.lower() in ['p', 'partial_link']: 32 element = wait.until(lambda x: x.find_element(by=By.PARTIAL_LINK_TEXT, value=locatorExpression)) 33 elif locationType.lower() in ['t', 'tag_name']: 34 element = wait.until(lambda x: x.find_element(by=By.TAG_NAME, value=locatorExpression)) 35 elif locationType.lower() in ['x', 'xpath']: 36 element = wait.until(lambda x: x.find_element(by=By.XPATH, value=locatorExpression)) 37 elif locationType.lower() in ['s', 'css']: 38 element = wait.until(lambda x: x.find_element(by=By.CSS_SELECTOR, value=locatorExpression)) 39 return element 40 except (NoSuchElementException, TimeoutException): 41 traceback.print_exc() 42 except Exception as e: 43 raise e 44 45 46 def findElements(driver, selector, second=30): 47 """获取多个相同页面元素对象,以list返回""" 48 wait = WebDriverWait(driver, second, 0.1) 49 try: 50 if '=>' not in selector: 51 elements = wait.until(lambda x: x.find_elements(by=By.ID, value=selector)) 52 return elements 53 locationType = selector.split('=>')[0] 54 locatorExpression = selector.split('=>')[1] 55 if locationType.lower() in ['i', 'id']: 56 elements = wait.until(lambda x: x.find_elements(by=By.ID, value=locatorExpression)) 57 elif locationType.lower() in ['n', 'name']: 58 elements = wait.until(lambda x: x.find_elements(by=By.NAME, value=locatorExpression)) 59 elif locationType.lower() in ['c', 'class']: 60 elements = wait.until(lambda x: x.find_elements(by=By.CLASS_NAME, value=locatorExpression)) 61 elif locationType.lower() in ['l', 'link']: 62 elements = wait.until(lambda x: x.find_elements(by=By.LINK_TEXT, value=locatorExpression)) 63 elif locationType.lower() in ['p', 'partial_link']: 64 elements = wait.until(lambda x: x.find_elements(by=By.PARTIAL_LINK_TEXT, value=locatorExpression)) 65 elif locationType.lower() in ['t', 'tag_name']: 66 elements = wait.until(lambda x: x.find_elements(by=By.TAG_NAME, value=locatorExpression)) 67 elif locationType.lower() in ['x', 'xpath']: 68 elements = wait.until(lambda x: x.find_elements(by=By.XPATH, value=locatorExpression)) 69 elif locationType.lower() in ['s', 'css']: 70 elements = wait.until(lambda x: x.find_elements(by=By.CSS_SELECTOR, value=locatorExpression)) 71 return elements 72 except (NoSuchElementException, TimeoutException): 73 traceback.print_exc() 74 except Exception as e: 75 raise e 76 77 78 if __name__ == "__main__": 79 from selenium import webdriver 80 from config.VarConfig import chromeDriverFilePath 81 driver = webdriver.Chrome(executable_path=chromeDriverFilePath) 82 driver.get("http://www.baidu.com") 83 searchBox = findElement(driver,"id=>kw") 84 print(searchBox.tag_name) 85 aList = findElements(driver,"t=>a") 86 print(len(aList)) 87 driver.quit()