def close(self): try: self.driver.close() logger.info("关闭并退出浏览器") except Exception as e: logger.error("退出浏览器,失败%s" % e) self.get_windows_img()
def wait_alert(self): try: WebDriverWait(self.driver,15).until(EC.alert_is_present()) logger.info("had wait for the alert") except TimeoutError as e: logger.error("can't wait for alert:%s"%e) self.get_windows_img()
def load_keras_sequential(relative_path: str, file_name_without_extension: str) -> Sequential: """ Loads a Keras Sequential neural network from file system Args: relative_path : relative path in project file_name_without_extension : file name without extension, will be used for json with models and h5 with weights. Returns: Sequential, or None if nothing found or error """ model_filename_with_path = os.path.join(ROOT_DIR, relative_path, file_name_without_extension + '.json') weights_filename_with_path = os.path.join(ROOT_DIR, relative_path, file_name_without_extension + '.h5') if os.path.exists(model_filename_with_path) and os.path.exists(weights_filename_with_path): try: json_file = open(model_filename_with_path, 'r') loaded_model_json = json_file.read() json_file.close() model = model_from_json(loaded_model_json) model.load_weights(weights_filename_with_path) logger.debug(f"load_keras_sequential: Loaded Sequential from {model_filename_with_path} " f"and {weights_filename_with_path}!") return model except: logger.error(f"load_keras_sequential: Loading of Sequential {model_filename_with_path} failed!") return None else: logger.error(f"load_keras_sequential: model File {model_filename_with_path} " f"or weights file {weights_filename_with_path} not found!") return None
def exit(self): try: self.click(*self.exit_button) logger.info("退出登录成功") except Exception as e: logger.error("退出失败%s" % e) self.get_windows_img()
def wait_eltext(self,locator,text): #locator内容需要括号括起来 try: WebDriverWait(self.driver,15).until(EC.text_to_be_present_in_element(locator,text)) logger.info('had wait for the text loaded:%s'%text) except TimeoutError as e: logger.error("can't wait for the text loaded:%s"%e) self.get_windows_img()
def window_handles(self, i): try: self.driver.switch_to.window(self.driver.window_handles[i]) logger.info("激活窗口成功") except Exception as e: logger.error("激活窗口失败%s" % e) self.get_windows_img()
def wait_elvalue(self,locator,text): try: WebDriverWait(self.driver,15).until(EC.text_to_be_present_in_element_value(locator,text)) logger.info('had wait for the input value:%s'%text) except TimeoutError as e: logger.error("can't wait for the input value:%s"%e) self.get_windows_img()
def capture_page(self, filename: str = '001.png'): try: file_path = os.path.join(CAPTURE_PATH, filename) self.driver.execute_script(""" (function () { var y = 0; var step = 100; window.scroll(0, 0); function f() { if (y < document.body.scrollHeight) { y += step; window.scroll(0, y); setTimeout(f, 50); } else { window.scroll(0, 0); document.title += "scroll-done"; } } setTimeout(f, 1000); })(); """) time.sleep(2) self.driver.save_screenshot(filename=file_path) logger.info(f'save screenshot success, save path: {file_path}') except Exception as e: logger.error(f'save screenshot failed, error code: {e}')
def save_keras_sequential(model: Sequential, relative_path: str, file_name_without_extension: str) -> bool: """ Saves a Keras Sequential in File System Args: model : Sequential to save relative_path : relative path in project file_name_without_extension : file name without extension, will be used for json with models and h5 with weights. Returns: True if successful, False otherwise, never None """ if model.model is None: logger.error(f"save_keras_sequential: Cannot write an empty model as file") return False try: model_as_json = model.to_json() model_filename_with_path = os.path.join(ROOT_DIR, relative_path, file_name_without_extension + '.json') weights_filename_with_path = os.path.join(ROOT_DIR, relative_path, file_name_without_extension + '.h5') json_file = open(model_filename_with_path, "w") json_file.write(model_as_json) json_file.close() model.save_weights(weights_filename_with_path) logger.debug(f"save_keras_sequential: Saved Sequential from {model_filename_with_path} " f"and {weights_filename_with_path}!") return True except: logger.error(f"save_keras_sequential: Writing of Sequential as file failed") return False
def wait_elements(self,locator): try: WebDriverWait(self.driver,15).until(EC.presence_of_all_elements_located(locator)) logger.info("had wait for all elements") except TimeoutError as e: logger.error("can't wait for elements:%s"%e) self.get_windows_img()
def forum(self): try: self.click(*self.forum_link) logger.info("点击论坛成功") except Exception as e: logger.error("点击论坛失败%s" % e) self.get_windows_img()
def double_click(self,selector): element = self.find_element(selector) try: ActionChains(self.driver).double_click(element).perform() logger.info("double click on the element") except NameError as e: logger.error("can't double click:%s" % e)
def scrollby_element(self,selector): el = self.find_element(selector) try: self.driver.excute_script("arguments[0].scrollIntoView(true);",el) logger.info("had scroll to the element target ") except NameError as e: logger.error("can't scoll to the element:%s"%e)
def wait_gonextframe(self, locator): try: WebDriverWait(self.driver, 15).until(EC.frame_to_be_available_and_switch_to_it(locator)) logger.info("had wait and changed frame") except TimeoutError as e: logger.error("can't wait for frame:%s" % e) self.get_windows_img()
def mouse_stop(self, selector): element = self.find_element(selector) try: ActionChains(self.driver).move_to_element(element).perform() logger.info("mouse stop on the element") except NameError as e: logger.error("can't mouse stop:%s"%e)
def click_defaul(self): try: self.click(*self.defaul_link) logger.info("成功点击默认版块") except Exception as e: logger.error("点击默认版块失败%s" % e) self.get_windows_img()
def get_value(self): try: # ss_list=self.find_elements(self.s) # per_list=self.find_elements(self.p) one_s = self.find_element(*self.s_one).text two_s = self.find_element(*self.s_two).text three_s = self.find_element(*self.s_three).text one_per = self.find_element(*self.per_one).text two_per = self.find_element(*self.per_two).text three_per = self.find_element(*self.per_three).text self.s_list = [one_s, two_s, three_s] self.per_list = [one_per, two_per, three_per] # print("名称%s"%one_s) # s_list=[] # p_list=[] # self.a = self.find_elements(*self.s) # self.b = self.find_elements(*self.p) # for j in range(0,len(self.a)): # s_list.append(self.a[j].text) # for p in range(0,len(self.b)): # p_list.append(self.b[p].text) tit = self.find_element(*self.main_tit).text print("标题:", tit) for i in range(0, len(self.s_list)): print("名称:{}\n比例:{}".format(self.s_list[i], self.per_list[i])) logger.info("成功获取标题 名称 比例") except Exception as e: logger.error("获取标题 名称 比例失败%s" % e) self.get_windows_img()
def wait_element(self, locator): try: El = WebDriverWait(self.driver,15).until(EC.presence_of_element_located(locator)) text = El.text logger.info("had wait for the element: %s"%text) except TimeoutError as e: logger.error("can't wait fort the element:%s"%e) self.get_windows_img()
def wait_elclickable(self,locator): try: El = WebDriverWait(self.driver,15).until(EC.element_to_be_clickable(locator)) text = El.text logger.info("had wait for the elclickable: %s"%text) except TimeoutError as e: logger.error("can't wait fort the elclickable:%s"%e) self.get_windows_img()
def get_input_text(self,selector): el = self.find_element(selector) try: input_text = el.get_attribute('value') logger.info("the input_text is:%s"% input_text) return input_text except NameError as e: logger.error("failed to get input value:%s"%e)
def new_re(self, reply): try: self.send_keys(reply, *self.reply) self.click(*self.reply_button) logger.info("普通用户回帖成功") except Exception as e: logger.error("普通用户回帖失败%s" % e) self.get_windows_img()
def exit(self): try: self.click(*self.exit_button) logger.info("退出登录成功") return self.find_element(*self.user).text except Exception as e: logger.error("退出失败%s" % e) self.get_windows_img()
def clear(self, *loc): el = self.find_element(*loc) try: el.clear() logger.info("清除文本框的内容") except Exception as e: logger.error("文本框内容清除失败%s" % e) self.get_windows_img()
def click_vote(self): try: self.click(*self.ele) self.click(*self.sss_vote) logger.info("成功点击发起投票") except Exception as e: logger.error("点击发起投票失败%s" % e) self.get_windows_img()
def get_haotest_tit(self): try: res = self.find_element(*self.result).text logger.info("成功获取得到的内容") return res except Exception as e: logger.error("获取得到的内容失败%s" % e) self.get_windows_img()
def click_haotest(self): try: self.click(*self.hao_cli) self.window_handles(2) logger.info("成功点击搜索出的内容") except Exception as e: logger.error("点击搜索出的内容失败%s" % e) self.get_windows_img()
def get_element_text(self, selector): el = self.find_element(selector) try: element_text = el.text logger.info("The element_text is: %s" % element_text) return element_text except NameError as e: logger.error("failed to get text:%s"%e)
def ex(self): try: self.current_window_handle() self.click(*self.exi) logger.info("退出管理中心成功") except Exception as e: logger.error("退出管理中心失败%s" % e) self.get_windows_img()
def click(self, *loc): el = self.find_element(*loc) try: el.click() logger.info("%s元素被点击" % el) except Exception as e: logger.error("%s元素点击失败" % e) self.get_windows_img()
def click_man(self): try: self.click(*self.manager_center) self.window_handles(1) logger.info("点击管理中心成功") except Exception as e: logger.error("点击管理中心失败%s" % e) self.get_windows_img()