# -*- coding:utf-8 -*- # date = 2019/7/25 # name = "yedeng" # project_name = gddn-ui-selenium # explain :元素定位方式說明,用於返回By類型 import sys from utils.log import Log from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By class FindBy: def __init__(self, Type): """ 用於返回loaction元組的by類型 :param Type: yaml文件的Type值 """ self.__Type = Type # 定義查找元素 def find_by(self) -> By: """ 經過'id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath'查找相應的locationby :rtype locationby : selenium.webdriver.common.by.By :return locationby 對應的location元組的By的值 """ """定位元素""" by = self.__Type locationby = None if by in ['id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath']: try: if by == 'id': locationby = By.ID elif by == 'name': locationby = By.NAME elif by == 'class': locationby = By.CLASS_NAME elif by == 'tag': locationby = By.TAG_NAME elif by == 'link': locationby = By.LINK_TEXT elif by == 'plink': locationby = By.PARTIAL_LINK_TEXT elif by == 'css': locationby = By.CSS_SELECTOR elif by == 'xpath': locationby = By.XPATH else: Log.error("沒有找到對應元素Type類型,請使用'id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath'") # Log.info('元素定位成功。定位方式:%s,使用的值%s:' % (by, value)) return locationby except NoSuchElementException as e: # print("no suceelement") method = sys._getframe(1).f_code.co_name # 調用該函數的函數名字,若是沒有被調用,則返回<module> path = sys._getframe(1).f_code.co_filename # 調用該函數的文件名字,若是沒有被調用,則返回<module> Log.error("在執行【" + path + "】文件中的【" + method + "】方法時出現錯誤" + "沒有找到對應的元素" + e.__str__()) else: # print("輸入的元素定位方式錯誤") Log.error("輸入的元素定位方式錯誤,請使用'id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath'")