def click(self, selector): s =self.presence_of_element_located(selector) try: s.click () log.info ("The element was clicked." ) except NameError as e: log.error ("Failed to click the element with %s" % e)
def get_col_info(self, key): df = self.df try: log.info('【%s】列信息显示如下:\n%s' % (key, df[key])) except KeyError: log.error('【%s】不是列名,请重新查看' % key) return df[key]
def clear_text(self, selector): el = self.presence_of_element_located(selector) try: el.clear () log.info ("Clear text in input box before typing.") except NameError as e: log.error ("Failed to clear in input box with %s" % e) self.get_windows_img ()
def enter_key(self, selector): e1 = self.find_element (selector) try: e1.send_keys (Keys.ENTER) log.info ("Had touch the Enter keys .") except Exception as e: log.error ("Failed to touch any keys") self.get_windows_img ()
def get_col_index(self, key): column = [ list(df.columns).index(v) for v in list(df.columns) if key in v ] try: print(column) print('根据key【%s】获取列号为:%s' % (key, column[0])) except Exception as e: log.error('列中无该值存在!%s' % e) return column[0]
def get_row_index(self, key): row = [ i for i in range(len(list(df.values))) for v in list(df.values)[i] if key in str(v) ] try: print(row) print('根据key【%s】获取行号为:%s' % (key, row[0])) except Exception as e: log.error('行中无该值存在!') return row[0]
def get_sheet(self): try: if not isinstance(self.sheet_str, str): log.error('s_name为字符串,不能为其他信息') self.sheetname = [ s for s in self.workbook.sheet_names() if self.sheet_str in s ][0] log.info('现在读取的是%s中的%s【sheet】' % (self.path, self.sheetname)) except Exception as e: log.error('s_name[%s]错误或者不存在!!!%s' % (self.sheet_str, e)) return self.sheetname
def get_windows_img(self): """ 在这里我们把file_path这个参数写死,直接保存到我们项目根目录的一个文件夹.\Screenshots下 """ file_path = os.path.dirname (os.path.dirname (__file__)) + '/screenshots/' rq = time.strftime ('%Y%m%d%H%M', time.localtime (time.time ())) screen_name = file_path + rq + '.png' try: self.driver.get_screenshot_as_file (screen_name) log.info ("Had take screenshot and save to folder : /screenshots") except NameError as e: log.error ("Failed to take screenshot! %s" % e) self.get_windows_img ()
def get_element_row_num(self, key): df = self.df key_pattern = r'^%s$' % key row = [] try: column = [i for i in df.columns if '页面元素' in i][0] row = [ k for k, v in df[column].items() if re.findall(key_pattern, v) ][0] log.info('第【%s】行' % row) except IndexError: log.error('错误,该行不存在,%s' % df.columns) return row
def type(self, selector, text): e1 = self.presence_of_element_located(selector) e1.clear () if e1.text: text1 = e1.text else: text1 = e1.get_attribute ('value') try: e1.send_keys (text) log.info ("Had type [ %s ] in inputBox" % text) except NameError as e: log.error ("Failed to type in input box with %s" % text1) log.error ("Failed %s" % e) self.get_windows_img ()
def find_elements(self, selector): global elements if '=>' not in selector: return self.driver.find_elements_by_id(selector) selector_by = selector.split('=>')[0] selector_value = selector.split('=>')[1] if selector_by == "i" or selector_by == 'id': try: elements = self.driver.find_elements_by_id(selector_value) s = element.tag_name log.info("Had find the elements %s successful " "by %s via value: %s " % (s, selector_by, selector_value)) except NoSuchElementException as e: log.error("NoSuchElementException: %s" % e) self.get_windows_img() elif selector_by == "n" or selector_by == 'name': elements = self.driver.find_elements_by_name(selector_value) elif selector_by == "c" or selector_by == 'class_name': elements = self.driver.find_elements_by_class_name(selector_value) elif selector_by == "l" or selector_by == 'link_text': elements = self.driver.find_elements_by_link_text(selector_value) elif selector_by == "p" or selector_by == 'partial_link_text': elements = self.driver.find_elements_by_partial_link_text( selector_value) elif selector_by == "t" or selector_by == 'tag_name': elements = self.driver.find_elements_by_tag_name(selector_value) elif selector_by == "x" or selector_by == 'xpath': try: elements = self.driver.find_elements_by_xpath(selector_value) s = [ kname.text for kname in self.driver.find_elements_by_xpath( selector_value) ] log.info("Had find the elements [ %s ] successful " "by %s via value: %s " % (s, selector_by, selector_value)) except NoSuchElementException as e1: log.error("NoSuchElementException: %s" % e1) self.get_windows_img() elif selector_by == "c" or selector_by == 'css': elements = self.driver.find_elements_by_css_selector( selector_value) else: raise NameError("Please enter a valid type of targeting elements.") return elements
def close(self): try: self.driver.close () log.info ("Closing and quit the browser.") except NameError as e: log.error ("Failed to quit the browser with %s" % e)