def get_elements(self, locator, doc=""): logger.info("{0}:查找所有匹配的元素:{1}".format(doc, locator)) try: return self.driver.find_elements(*locator) except: logger.exception("查找元素失败!!!") self.save_screenshot(doc) raise
def alert_action(self, doc=""): ele = self.driver.switch_to.alert try: ele.accept() except: logger.exception("弹窗关闭失败!!!") self.save_screenshot(doc) raise
def select_text(self, locator, text, times=15, poll_frequency=0.5, doc=""): self.wait_eleVisible(locator, times, poll_frequency, doc) ele = self.get_element(locator, doc) logger.info("{0}:下拉框text选择元素:{1}".format(doc, locator)) try: Select(ele).select_by_visible_text(text) except: logger.exception("元素下拉框通过text选择元素操作失败!!!") self.save_screenshot(doc) raise
def click_element(self, locator, times=15, poll_frequency=0.5, doc=""): self.wait_eleVisible(locator, times, poll_frequency, doc) ele = self.get_element(locator, doc) logger.info("{0}:点击元素:{1}".format(doc, locator)) try: ele.click() except: logger.exception("元素点击操作失败!!!") self.save_screenshot(doc) raise
def get_text(self, locator, times=15, poll_frequency=0.5, doc=""): self.wait_eleVisible(locator, times, poll_frequency, doc) ele = self.get_element(locator, doc) logger.info("在{0}获取元素:{1}的文本内容".format(doc, locator)) try: return ele.text except: logger.exception("获取元素文本内容失败!!!") self.save_screenshot(doc) raise
def input_text(self, locator, text, times=15, poll_frequency=0.5, doc=""): self.wait_eleVisible(locator, times, poll_frequency, doc) ele = self.get_element(locator, doc) logger.info("{0}:输入操作:{1}".format(doc, locator)) try: ele.send_keys(text) except: logger.exception("元素输入操作失败!!!") self.save_screenshot(doc) raise
def wait_elePresence(self, locator, times=15, poll_frequency=0.5, doc=""): logger.info("{0}:等待元素{1}存在".format(doc, locator)) try: start = datetime.datetime.now() WebDriverWait(self.driver, times, poll_frequency).until( EC.presence_of_element_located(locator)) end = datetime.datetime.now() wait_time = (end - start).seconds logger.info("等待结束,等待时长为:{}秒".format(wait_time)) except: logger.exception("等待元素存在失败!!!") self.save_screenshot(doc) raise
def wait_eleVisible(self, locator, times=15, poll_frequency=0.5, doc=""): logger.info("{}:等待元素{}可见".format(doc, locator)) try: start = datetime.datetime.now() WebDriverWait(self.driver, times, poll_frequency).until( EC.visibility_of_element_located(locator)) end = datetime.datetime.now() wait_time = (end - start).seconds logger.info( "{0} :元素{1}已可见 ,等待起始时间:{2},等待结束时间:{3}, 等待时长为:{4}秒".format( doc, locator, start, end, wait_time)) except: logger.exception("等待元素可见失败!!!") self.save_screenshot(doc) raise