def _view_tips(self): content = "" try: tips_open = self.app.driver.find_element_by_xpath( rules["daily_tips_open"]) tips_open.click() except NoSuchElementException as e: logger.debug("没有可点击的【查看提示】按钮") return "" time.sleep(2) try: tips = self.app.wait.until( EC.presence_of_element_located( (By.XPATH, rules["daily_tips"]))) content = tips.get_attribute("name") logger.debug(f'提示 {content}') except NoSuchElementException as e: logger.error(f'无法查看提示内容') return "" time.sleep(2) try: tips_close = self.app.driver.find_element_by_xpath( rules["daily_tips_close"]) tips_close.click() except NoSuchElementException as e: logger.debug("没有可点击的【X】按钮") time.sleep(2) return content
def _special(self): self.app.safe_click(rules["special_entry"]) self.app.safe_click(rules["special_current"]) time.sleep(5) for i in range(10): logger.debug(f'专项答题 第 {i+1} 题') try: category = self.app.driver.find_element_by_xpath( rules["special_category"]).get_attribute("name") print(category) except NoSuchElementException as e: logger.error(f'无法获取题目类型') raise e if "填空题 (10分)" == category: self.daily._blank() elif "单选题 (10分)" == category: self.daily._radio() elif "多选题 (10分)" == category: self.daily._check() else: logger.error(f"未知的题目类型: {category}") raise ('未知的题目类型') logger.debug(f'专项答题循环结束') self.app.safe_back('speical report -> special list') self.app.safe_back('special list -> quiz')
def find_elements(self, ele:str): logger.debug(f'find elements by xpath: {ele}') try: elements = self.driver.find_elements_by_xpath(ele) except NoSuchElementException as e: logger.error(f'找不到元素: {ele}') raise e return elements
def _dispatch(self, count_of_each_group): time.sleep(3) # 如果模拟器比较流畅,这里的延时可以适当调短 for i in range(count_of_each_group): logger.debug(f'正在答题 第 {i+1} / {count_of_each_group} 题') try: category = self.app.driver.find_element_by_xpath( rules["daily_category"]).get_attribute("name") except NoSuchElementException as e: logger.error(f'无法获取题目类型') raise e print(category) if "填空题" == category: self._blank() elif "单选题" == category: self._radio() elif "多选题" == category: self._check() else: logger.error(f"未知的题目类型: {category}")
def login_or_not(self): # com.alibaba.android.user.login.SignUpWithPwdActivity time.sleep(10) # 首屏等待时间 try: home = self.driver.find_element_by_xpath(rules["home_entry"]) logger.debug(f'不需要登录') return except NoSuchElementException as e: logger.debug(self.driver.current_activity) logger.debug(f"非首页,先进行登录") if not self.username or not self.password: logger.error(f'未提供有效的username和password') logger.info(f'也许你可以通过下面的命令重新启动:') logger.info(f'\tpython -m xuexi -u "your_username" -p "your_password"') raise ValueError('需要提供登录的用户名和密钥,或者提前在App登录账号后运行本程序') username = self.wait.until(EC.presence_of_element_located(( By.XPATH, rules["login_username"] ))) password = self.wait.until(EC.presence_of_element_located(( By.XPATH, rules["login_password"] ))) username.send_keys(self.username) password.send_keys(self.password) self.safe_click(rules["login_submit"]) time.sleep(8) try: home = self.driver.find_element_by_xpath(rules["home_entry"]) logger.debug(f'无需点击同意条款按钮') return except NoSuchElementException as e: logger.debug(self.driver.current_activity) logger.debug(f"需要点击同意条款按钮") self.safe_click(rules["login_confirm"]) time.sleep(3)
def _kaleidoscope(self): ''' 本地频道积分 +1 ''' if self.app.back_or_not("本地频道"): return volumns = self.app.wait.until( EC.presence_of_all_elements_located( (By.XPATH, rules['article_volumn']))) volumns[3].click() time.sleep(10) # self.safe_click(rules['article_kaleidoscope']) target = None try: target = self.app.driver.find_element_by_xpath( rules['article_kaleidoscope']) except NoSuchElementException as e: logger.error(f'没有找到城市万花筒入口') if target: target.click() time.sleep(3) delay = random.randint(5, 15) logger.info(f"在本地学习平台驻足 {delay} 秒") time.sleep(delay) self.app.safe_back('学习平台 -> 文章列表')
def _blank(self): contents = self.app.wait.until( EC.presence_of_all_elements_located( (By.XPATH, rules["daily_blank_content"]))) # contents = self.find_elements(rules["daily_blank_content"]) # content = " ".join([x.get_attribute("name") for x in contents]) logger.debug(f'len of blank contents is {len(contents)}') if 1 < len(contents): # 针对作妖的UI布局某一版 content, spaces = "", [] for item in contents: content_text = item.get_attribute("name") if "" != content_text: content += content_text else: length_of_spaces = len( item.find_elements(By.CLASS_NAME, "android.view.View")) - 1 spaces.append(length_of_spaces) content += " " * (length_of_spaces) else: # 针对作妖的UI布局某一版 contents = self.app.wait.until( EC.presence_of_all_elements_located( (By.XPATH, rules["daily_blank_container"]))) content, spaces, _spaces = "", [], 0 for item in contents: content_text = item.get_attribute("name") if "" != content_text: content += content_text if _spaces: spaces.append(_spaces) _spaces = 0 else: content += " " _spaces += 1 else: # for...else... # 如果填空处在最后,需要加一个判断 if _spaces: spaces.append(_spaces) logger.debug( f'[填空题] {content} [{" ".join([str(x) for x in spaces])}]') logger.debug(f'空格数 {spaces}') blank_edits = self.app.wait.until( EC.presence_of_all_elements_located( (By.XPATH, rules["daily_blank_edits"]))) # blank_edits = self.find_elements(rules["daily_blank_edits"]) length_of_edits = len(blank_edits) logger.info(f'填空题 {content}') answer = self._verify("填空题", content, []) # if not answer: words = (''.join( random.sample(string.ascii_letters + string.digits, 8)) for i in range(length_of_edits)) else: words = answer.split(" ") logger.debug(f'提交答案 {words}') for k, v in zip(blank_edits, words): k.send_keys(v) time.sleep(1) self._submit() try: wrong_or_not = self.app.driver.find_element_by_xpath( rules["daily_wrong_or_not"]) right_answer = self.app.driver.find_element_by_xpath( rules["daily_answer"]).get_attribute("name") answer = re.sub(r'正确答案: ', '', right_answer) logger.info(f"答案 {answer}") notes = self.app.driver.find_element_by_xpath( rules["daily_notes"]).get_attribute("name") logger.debug(f"解析 {notes}") self._submit(2) if 1 == length_of_edits: localmodel.update_bank('挑战题', content, [""], answer, '', notes) else: logger.error("多位置的填空题待验证正确性") localmodel.update_bank( '填空题', content, [""], self._blank_answer_divide(answer, spaces), '', notes) except: logger.debug("填空题回答正确")