def find_element1(self, selectors): element = '' selector_by = selectors[0] # 元素名称 selector_value = selectors[1] # 元素ID名称 try: # 显示等待,判断元素是否存在 WebDriverWait( # 传入浏览器对象 driver=self.driver, # 传入超时时间 timeout=TimeOut, # 传入轮询时间 poll_frequency=PollFrequency).until( # 检测定位的元素是否可见,这里接受的就是个元组作为参数,所以不需要解包 EC.visibility_of_element_located(selectors)) except TimeoutException as e: logger.error("%s 查找元素超时" % get_dataTime()) if selector_by == "i" or selector_by == "id": try: element = self.driver.find_element_by_id( selector_value) # id 定位 logger.info("%s Had find the element \' %s \' successfully " "by %s via value:%s" % (get_dataTime(), element.text, selector_by, selector_value)) except NoSuchElementException as e: logger.error("%s NoSuchElementException:%s" % (get_dataTime(), e)) self.get_windows_img() return element
def get_windows_img(self): """ 截图 :return: """ file_path = os.path.abspath('.') + '\\screenshots\\' isExists = os.path.exists(file_path) # print(f"截图位置:{file_path}") # 判断文件夹是否存在,如果不存在则创建。 if not isExists: try: os.makedirs(file_path) except Exception as e: logger.error("%s Failed new bulid folder %s" % (get_dataTime(), e)) # # 获取时间作为字符串,用作保存图片的名称 # rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time())) rq = get_dataTime('%Y%m%d%H%M%S') screen_name = file_path + rq + '.png' try: self.driver.get_screenshot_as_file(screen_name) logger.info( "%s Had take screenshot and save to folder : /screenshots" % get_dataTime()) except NameError as e: logger.error("%s Failed to take screenshot! %s" % (get_dataTime(), e)) self.get_windows_img()
def clear(self, selector): el = self.find_element(selector) # 获取元素位置信息 try: el.clear() logger.info("%s Clear text in input box before type" % get_dataTime()) except NameError as e: logger.error("%s Failed to type in input box with %s" % (get_dataTime(), e)) self.get_windows_img()
def click(self, selector): el = self.find_element(selector) # 获取元素位置信息 try: el.click() logger.info("%s The emement was click" % get_dataTime()) # 并不是每个元素都存在 text 属性 except NameError as e: logger.error("%s Failed to type in input box with %s" % (get_dataTime(), e)) self.get_windows_img()
def close_browser(self): """ 关闭当前浏览器窗口 :return: """ try: self.driver.close() logger.info("%s Close and quit the browser" % get_dataTime()) except NameError as e: logger.error("% Failed to quit the browser with %s" % (get_dataTime(), e))
def send_keys(self, selector, text): el = self.find_element(selector) # 获取元素位置信息 el.clear() # 文本框清空 try: el.send_keys(text) # 输入文本信息 logger.info("%s Had type \' %s \' in inputBox" % (get_dataTime(), text)) except NameError as e: logger.error("%s Failed to type in input box with %s" % (get_dataTime(), e)) self.get_windows_img()
def move_to_element(self, selector): """ 鼠标移入某个元素上 :param selector: :return: """ el = self.find_element(selector) # 获取元素位置信息 try: ActionChains(self.driver).move_to_element(el).perform() logger.info("%s The mouse move on %s" % (get_dataTime(), el.text)) except NameError as e: logger.error("%s Failed to the mouse move on %s " % (get_dataTime(), e)) self.driver.get_windows_img()
def forEach_send_keys(self, selector_objects, text): """ 给一组输入框遍历输入内容 :param selector_objects: :param text: :return: """ for ele in selector_objects: ele.clear() # 文本框清空 try: ele.send_keys(text) # 输入文本信息 logger.info("%s Had type \' %s \' in inputBox" % (get_dataTime(), text)) except NameError as e: logger.error("%s Failed to type in input box with %s" % (get_dataTime(), e)) self.get_windows_img()
def get_url_title(self): """ get_url_title 获取当前网页标题 :return: """ logger.info("%s Current page title is %s" % (get_dataTime(), self.driver.title)) return self.driver.title
def create_Str(text): """ 生成以text开头,以当前时间结尾的字符串 :param text: :return: """ return text + get_dataTime(time_formate="%Y%m%d%H%M%S")
def window_handles(self): """ 获取所有窗口句柄 :return: """ logger.info("%s Get all window handles" % get_dataTime()) return self.driver.window_handles
def back_browser(self): """ back browser 浏览器后退方法 :return: """ self.driver.back() logger.info("%s Click back to current page." % get_dataTime())
def switch_to_new_window(self): """ 切换到当前最新打开的窗口 :return: """ logger.info("%s Switch to the current latest window" % get_dataTime()) self.driver.switch_to.window(self.window_handles()[-1])
def send_keys(self, selector, text): """ 文本输入框中输入内容 :param selector: :param text: :return: """ el = self.find_element(selector) # 获取元素位置信息 # 先对文本框清空 el.clear() try: el.send_keys(text) # 输入文本信息 logger.info("%s Had type \' %s \' in inputBox" % (get_dataTime(), text)) except NameError as e: logger.error("%s Failed to type in input box with %s" % (get_dataTime(), e)) self.get_windows_img()
def forward_browser(self): """ forward browser 浏览器前进方法 :return: """ self.driver.forward() logger.info("%s Click forward on current page." % get_dataTime())
def refresh(self): """ 刷新当前页面 :return: """ logger.info("%s The page has been refreshed" % get_dataTime()) self.driver.refresh()
def switch_to_alert(self): """ 切换到弹窗窗口 :return: """ logger.info("%s Switch to pop-up window" % get_dataTime()) return self.driver.switch_to.alert()
def send_CDP_recTime(self, time=get_dataTime("%Y-%m-%d %H:%M:%S")): """ 已收回款弹窗:输入回款时间 :param time: 默认当前时间 :return: """ self.send_keys(self.CDP_recTimeInp_input, time)
def current_window_handle(self): """ 获取当前窗口句柄 :return: """ logger.info("%s Get the current window handle" % get_dataTime()) return self.driver.current_window_handle
def quit_browser(self): """ 浏览器退出方法 :return: """ logger("%s quit the browser!" % get_dataTime()) self.driver.quit()
def send_CDP_proAppTime(self): """ 输入立项时间,默认选择当月 :return: """ self.click(self.CDP_proAppTime_input) self.send_keys(self.CDP_proAppTime_input, get_dataTime(time_formate="%Y-%m"))
def alertDismiss(self, alert_ele): """ 点击取消按钮:alert.dismiss() :param alert_ele: :return: """ alert_ele.dismiss() logger.info("%s Clicked the Cancel button in the dialog window" % get_dataTime())
def alertAccept(self, alert_ele): """ alert对话框窗口,点击确定按钮:alert.accept() :param alert_ele: :return: """ alert_ele.accept() logger.info("%s Clicked the OK button in the dialog window" % get_dataTime())
def send_keys_remarks(self, text="测试完善合同:" + get_dataTime("%Y-%m-%d %H:%M:%S")): """ 输入备注 :param text: :return: """ self.send_keys(self.PC_remarks_textarea, text)
def send_keys_signTime(self, timeStr=get_dataTime("%Y-%m-%d %H:%M:%S")): """ 输入签约时间 :param timeStr: 默认选择当前时间 :return: """ # 选择签约时间 选择当前时间 self.send_keys(self.PC_signTimeStrInp, timeStr)
def slidingScrollbar(self, target): """ 滑动滚动条,到达指定的元素 :param target: :return: """ js = "arguments[0].scrollIntoView();" logger.info("%s Slide the scroll bar to the specified element: %s" % (get_dataTime(), js)) self.driver.execute_script(js, target)
def execute_async_script(self, js): """ 异步执行js脚本:execute_async_script为异步执行且执行时间较长。 WebDriver不会等待异步执行的结果,而是直接执行后续的代码 :param js: js脚本 :return: """ logger.info("%s Execute JS script asynchronously: %s" % (get_dataTime(), js)) return self.driver.execute_async_script(js)
def switch_to_iframe(self, selector): """ 切换到指定的iframe :param selector: iframe的WebElement对象 :return: """ iframe_object = self.find_element(selector) self.driver.switch_to.frame(iframe_object) logger.info("%s Switch to the iframe" % get_dataTime())
def execute_script(self, js): """ 同步执行js脚本:execute_script为同步执行且执行时间较短。 WebDriver会等待同步执行的结果然后执行后续代码; :param js: js脚本 :return: """ logger.info("%s Synchronous execution of JS script: %s" % (get_dataTime(), js)) return self.driver.execute_script(js)
def get_alert_text(self, alert_ele): """ 获取对话框窗口的提示文本 :param alert_ele: alert对话框元素 :return: """ alertText = alert_selector.text logger.info("%s Geted the prompt text to the dialog window: %s " % (get_dataTime(), alertText)) return alertText