def pre(self): """ 进入播放文稿页面下,用于点击上一页按钮 :return: 操作成功返回预览图对象,失败返回 False """ time.sleep(random.choice(targets)) for times in range(1, 4): try: click_element(self.driver, self.pre_xpath) logger.debug('在文稿播放的状态点击上一页') preview_xpath_elem = wait_for_find_element( self.driver, self.preview_xpath) src = preview_xpath_elem.get_property('src') logger.info('成功获取预览图的 src 属性(src属性 过长,省略。。。)') return src except TimeoutException as timeout: err_msg = '在文稿播放的状态点击上一页时超时失败:{},准备重试第{}次 '.format( timeout, times) logger.error(err_msg) self.screen_shot() except Exception as e: logger.error("发生未知的异常:{},准备进行重试".format(e)) self.screen_shot() else: logger.error('点击上一页按钮{}次了依然失败!!!'.format(times)) return False
def start(self): """ 用于点击启动播放按钮 :return: 点击成功就返回 True,否则 False """ for times in range(1, 4): try: click_element(self.driver, self.start_xpath) logger.debug('点击播放文稿按钮') play_title_elem = wait_for_find_element( self.driver, self.play_title_xpath) logger.debug('断言 start_title_elem.text({}) == 文稿预览'.format( play_title_elem.text)) assert play_title_elem.text.strip() == '文稿播放' logger.debug('成功启动文稿播放') return True except TimeoutException as timeout: err_msg = '点击播放文稿按钮出错: {},准备进行{}次重试'.format(timeout, times) logger.error(err_msg) self.screen_shot() except AssertionError as err: err_msg = '断言出错:{},准备进行{}次重试'.format(err, times) logger.error(err_msg) self.screen_shot() except Exception as e: logger.error("发生未知的异常:{},准备进行重试".format(e)) self.screen_shot() else: logger.fatal('重试了{}次还是启动播放失败'.format(times)) return False
def logout(self): """ 登出多屏控制端的具体方法 :return: 无返回值 """ try: if element_exist(self.driver, self.exit_play_xpath): click_element(self.driver, self.exit_play_xpath, 15) logger.debug('退出播放') if element_exist(self.driver, self.logout_xpath): click_element(self.driver, self.logout_xpath, 15) logger.debug('退出系统') if self.driver: self.driver.close() logger.debug('关闭浏览器') except TimeoutException: logger.error('登出操作超时!!!') self.screen_shot() if self.driver: self.driver.close() except Exception as e: logger.error("发生未知的异常:{}".format(e)) self.screen_shot() if self.driver: logger.info('关闭浏览器') self.driver.close()
def _check_out_california_campsite_page(driver, name, date, available_sites): time.sleep(.2) rows = driver.find_elements_by_css_selector("table>tbody>tr") for row in rows: children = row.find_elements_by_xpath(".//*") if children[2].get_attribute("class") == "blue_brd_box": driver.execute_script("arguments[0].scrollIntoView();", children[2]) site_name = children[1].text if not any(adj in site_name for adj in ["ADA", "Group", "Day"]): msg = "{}: {} is available".format(name, site_name) if msg not in available_sites[date]: available_sites[date].append(msg) time.sleep(.2) click_element(driver, '//*[@id="aHomeBlueiconh"]') return available_sites
def login(self): """ 登录多屏控制端的具体方法 :return: 无返回值 """ count = 0 while count < 9999: try: self.driver.get(self.url) logger.debug('打开主页,进入登录页') self.driver.maximize_window() logger.debug('最大化窗口') input_text(self.driver, self.username_xpath, self.username) logger.debug('在登录页输入用户名') input_text(self.driver, self.password_xpath, self.password) logger.debug('在登录页输入密码') click_element(self.driver, self.login_btn_xpath) logger.debug('点击登陆按钮') start_title_elem = wait_for_find_element( self.driver, self.start_title_xpath) logger.debug('断言 start_title_elem.text({}) == 文稿预览'.format( start_title_elem.text)) assert start_title_elem.text.strip() == '文稿预览' logger.debug('登陆成功') break except TimeoutException as timeout: count += 1 err_msg = '登录超时: {},准备进行第{}次重试 '.format(timeout, count) logger.error(err_msg) self.screen_shot() except AssertionError as err: count += 1 err_msg = '断言出错: {},当前页面下没有“文稿预览”元素 '.format(err) logger.error(err_msg) self.screen_shot() except Exception as e: count += 1 logger.error("发生未知的异常:{},准备进行重试".format(e)) if count >= 9999: logger.fatal('登陆重试{}次, 退出程序!!!'.format(count)) self.screen_shot() raise Exception('重试了 {} 次登陆都失败了,抛出异常!!!'.format(count))
def check_california_sites(driver, name, date, available_sites): # Starting page time.sleep(.2) enter_value(driver, SITE_PICKER_ID, name, select_on_dropdown=True) time.sleep(1) for i in ["Camping", "Remote Camping"]: try: select_dropdown_visible_text(driver, DROPDOWN_TYPE_ID, i) except Exception: pass time.sleep(.8) enter_value(driver, DATE_ARRIVAL_ID, date) time.sleep(.1) select_dropdown_visible_text(driver, NUMBER_OF_NIGHTS_ID, "1") time.sleep(1.2) click_element(driver, SEND_BUTTON_XPATH) time.sleep(2) # Page 2 return _get_available_sites_in_california_table(driver, name, date, available_sites)
def select_screen(self): """ 用于切换显示屏,切换成宝安中控的大屏 :return: 无返回值 """ click_element(self.driver, self.change_screen_xpath, 10) click_element(self.driver, self.select_screen_xpath) click_element(self.driver, self.select_screen_confirm_xpath)
def loop_play(self): """ 循环去播放一个文稿(一直按下一页下一页,到底后再按上一页上一页,如此反复) :return: 无返回值 """ for times in range(1, 11): print('loop_times: {}'.format(times)) try: img_src_list = [] while self.next_exist(): src = self.next() if src is False: logger.error('无法点击下一页元素,尝试去点击上一页') break img_src_list.append(src) if len(img_src_list) == 2: logger.info('进行两次操作后,准备断言前后两次操作的预览图不一致') assert img_src_list[0] != img_src_list[1] logger.info('断言成功,前后两次操作的预览图不一致') img_src_list.pop(0) img_src_list.clear() while self.pre_exist(): src = self.pre() if src is False: logger.error('无法点击上一页元素,尝试去点击下一页') break img_src_list.append(src) if len(img_src_list) == 2: logger.info('进行两次操作后,准备断言前后两次操作的预览图不一致') assert img_src_list[0] != img_src_list[1] logger.info('断言成功,前后两次操作的预览图不一致') img_src_list.pop(0) except AssertionError: logger.error('断言失败,原因可能是上一页或下一页按钮不可点击;' '又或者按钮点击了预览图没有更新,请看下方截图。') self.screen_shot() except StaleElementReferenceException as sere: logger.error('过期元素引用异常:{}'.format(sere)) self.screen_shot() except Exception as e: logger.error("发生未知的异常:{},准备进行重试".format(e)) self.screen_shot() if self.exit_play_exist(): try: click_element(self.driver, self.exit_play_xpath) logger.debug('退出播放') print('退出播放') start_title_elem = wait_for_find_element( self.driver, self.start_title_xpath, 20) logger.info('找到文稿预览元素,成功退出播放') logger.info('准备断言退出播放成功') assert start_title_elem.text.strip() == '文稿预览' logger.debug('断言退出播放成功') except TimeoutException: logger.error('无法找到文稿预览元素,已超时') print('无法找到文稿预览元素,已超时') self.screen_shot() except AssertionError: logger.error('断言退出播放失败') self.screen_shot() except Exception as e: logger.error("发生未知的异常:{},准备进行重试".format(e)) self.screen_shot()