In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
How to use by location elements to assist ReadyamlToElement classes? This article uses the example code to show, the code is very detailed, has a certain reference value, interested friends can refer to.
#-*-coding:utf-8-*-# date = 2019 coding:utf-8 5Universe name = "yedeng" # project_name = python# explain: WebElement element location, use import inspect for GetelementFromYaml, osfrom utils.log import Logfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.remote import webelementdef get_current_function_name (): return inspect.stack () [1] [3]''to verify the existence of the input file Need to improve''class Getyamlfile: def file_name (self) File_dir): list_dirs = os.listdir (file_dir) # iterate through all file names in the specified directory yamlfile = (os.path.split (_ file__) [- 1] .split (".") [0]) + ".yaml") # the current py file name + .yaml # print ("now" + (sys._getframe (). F_code.co) _ filename) .split ('.') [0]) if yamlfile in list_dirs: return yamlfile else: return None "" get the corresponding elemet improvement through key. Imagine: convert all yaml files to the corresponding webelement type key:WebElement expected problems: 1. The page is not finished loading, 2. The element 3.frame tag that appears by clicking on the page element may not locate the class FindWebElement: def _ _ init__ (self, driver, Type) to be verified. Value): "": type driver: selenium.webdriver.remote.webdriver.WebDriver instantiates the type value of the findWebElement: param driver:: param Type: yaml file: the value of the param Value: yaml file "self.__driver = driver self.__Type = Type self.__Value = Value # defines the lookup element def find_element (self, yamlpath Key)-> webelement: "" through 'id',' name', 'class',' tag', 'link',' plink', 'css' 'xpath' finds the corresponding element: return: WebElemt corresponds to "location element" by = self.__Type value = self.__Value element = None if by in [' id', 'name',' class', 'tag',' link', 'plink',' css' " 'xpath']: # noinspection PyBroadException try: if by = =' id': element = self.__driver.find_element_by_id (value) elif by = = 'name': element = self.__driver.find_element_by_name (value) elif by = 'class': element = self.__driver.find_element_by_class_name (value) elif by = =' tag': element = self.__driver.find_element_by_tag_name (value) elif by = 'link': element = self.__driver.find_element_by_link _ text (value) elif by = = 'plink': element = self.__driver.find_element_by_partial_link_text (value) elif by =' css': element = self.__driver.find_element_by_css_selector (value) elif by = = 'xpath': Element = self.__driver.find_element_by_xpath (value) else: Log.error ("No corresponding element Type type found Use 'id',' name', 'class',' tag', 'link',' plink', 'css',' xpath' ") # Log.info ('element positioned successfully. Positioning method:% s, value used% s'% by, value) return element except NoSuchElementException as e: # print ("no suceelement") Log.error ("yaml file is:" + yamlpath + "; there is no element on the page whose key value is'" + key + ";" + e.roomstringing _ () + "; please make sure the location is accurate.") # self.get_img () # call screenshot else: # print ("incorrect positioning of input element") Log.error ("incorrect positioning of input element, please use 'id',' name', 'class',' tag', 'link',' plink', 'css',' xpath'") def find_elements (self, yamlpath) Key)-> list: "find the corresponding element through 'id',' name', 'class',' tag', 'link',' plink', 'css',' xpath': return: return the selenium.webdriver.remote.webelement class directly when there is only one list_elements Otherwise, return list: rtype list_elements: list of WebElement "location element" by = self.__Type value = self.__Value list_elements = None if by in ['id',' name', 'class',' tag', 'link',' plink', 'css' " 'xpath']: # noinspection PyBroadException try: if by = =' id': list_elements = self.__driver.find_elements_by_id (value) elif by = 'name': list_elements = self.__driver.find_elements_by_name (value) Elif by = 'class': list_elements = self.__driver.find_elements_by_class_name (value) elif by =' tag': list_elements = self.__driver.find_elements_by_tag_name (value) elif by = 'link': list_elements = self._ _ driver.find_elements_by_link_text (value) elif by = = 'plink': list_elements = self.__driver.find_elements_by_partial_link_text (value) elif by = =' css': list_elements = self.__driver.find_elements_by_css_selector (value) elif By = 'xpath': list_elements = self.__driver.find_elements_by_xpath (value) else: Log.error ("corresponding element Type type not found Use 'id',' name', 'class',' tag', 'link',' plink', 'css',' xpath' ") # Log.info ('element positioned successfully. Positioning method:% s, value used% s'% (by, value) return list_elements except NoSuchElementException as e: # print ("no suceelement") Log.error ("yaml file is:" + yamlpath + "; there is no element in the page with a positioning key value of'" + key + ";" + e.roomstringing _ () + " Please make sure the location is accurate. ") # self.get_img () # call screenshot else: # print ("incorrect positioning of input elements") Log.error ("wrong positioning of input elements, please use 'id',' name', 'class',' tag', 'link',' plink', 'css',' xpath'")
The above are the details of the use of by positioning elements to assist the ReadyamlToElement class, and the detailed use needs to be used in a hands-on experiment in order to understand. If you want to know more, you are welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.