From 99e07c5753aaddd028dacdea2f9c20fa02792170 Mon Sep 17 00:00:00 2001 From: KirigiriSuzumiya <82195326+KirigiriSuzumiya@users.noreply.github.com> Date: Tue, 3 May 2022 18:22:49 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A8=B3=E5=AE=9A=E6=80=A7=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更改了等待逻辑,提高运行稳定性 --- README.md | 4 +-- XueXiTong_web.py | 73 ++++++++++++++++++++++++------------------------ 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index e6c4b17..10b297f 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ password = '' 示例: - ![网页捕获_3-5-2022_15181_mooc1.chaoxing.com](新建 文本文档 (2).assets/网页捕获_3-5-2022_15181_mooc1.chaoxing.com.jpeg) + ![网页捕获_3-5-2022_15181_mooc1.chaoxing.com](README.assets/网页捕获_3-5-2022_15181_mooc1.chaoxing.com.jpeg) 2. username @@ -77,4 +77,4 @@ password = '' 你的学习通密码 -**修改完成后直接运行就行,有什么问题看看命令行输出卡在哪里了。** +**修改完成后直接运行就行,有什么问题看看命令行输出卡在哪里了。** \ No newline at end of file diff --git a/XueXiTong_web.py b/XueXiTong_web.py index 8e6d06f..639b4cd 100644 --- a/XueXiTong_web.py +++ b/XueXiTong_web.py @@ -57,37 +57,43 @@ def get_answer(question_text): chrome.find_element(By.CLASS_NAME, "ipt-tel").send_keys(username) chrome.find_element(By.CLASS_NAME, "ipt-pwd").send_keys(password) chrome.find_element(By.ID, "loginBtn").click() -wait = WebDriverWait(chrome, 100) +wait = WebDriverWait(chrome, 10000) wait.until(EC.title_is("学习进度页面")) print("登陆成功!") while (True): - wait.until(EC.presence_of_all_elements_located) - chrome.refresh() - wait.until(EC.presence_of_all_elements_located) + wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, "orange"))) task_point = chrome.find_element(By.CLASS_NAME, "orange") task_point.click() - wait.until(EC.presence_of_all_elements_located) - wait.until(EC.presence_of_element_located((By.ID, "dct2"))) + wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'h1'))) print("章节标题:", chrome.find_element(By.TAG_NAME, 'h1').text) # 完成视频任务点 chrome.switch_to.frame(chrome.find_element(By.TAG_NAME, "iframe")) try: chrome.find_element(By.CLASS_NAME, "ans-job-finished") except: - print("开始播放视频") chrome.switch_to.frame(chrome.find_element(By.TAG_NAME, "iframe")) - element = chrome.find_element(By.ID, 'ext-comp-1041') - chrome.execute_script(""" - var element = arguments[0]; - element.parentNode.removeChild(element); - """, element) - element = chrome.find_element(By.ID, 'ext-comp-1042') - chrome.execute_script(""" - var element = arguments[0]; - element.parentNode.removeChild(element); - """, element) + try: + wait_flow = WebDriverWait(chrome, 2) + wait_flow.until(EC.presence_of_all_elements_located((By.ID, 'ext-comp-1041'))) + element = chrome.find_element(By.ID, 'ext-comp-1041') + chrome.execute_script(""" + var element = arguments[0]; + element.parentNode.removeChild(element); + """, element) + wait_flow.until(EC.presence_of_all_elements_located((By.ID, 'ext-comp-1042'))) + element = chrome.find_element(By.ID, 'ext-comp-1042') + chrome.execute_script(""" + var element = arguments[0]; + element.parentNode.removeChild(element); + """, element) + print("弹窗题目已屏蔽!") + except: + print("未发现有弹窗题目!") + pass chrome.execute_script("window.focus();") + wait.until(EC.presence_of_all_elements_located((By.XPATH, "//button[@class='vjs-big-play-button']"))) chrome.find_element(By.XPATH, "//button[@class='vjs-big-play-button']").click() + print("开始播放视频") while True: wait_video = WebDriverWait(chrome, 10000) wait_video.until(EC.presence_of_all_elements_located((By.CLASS_NAME, "vjs-paused"))) @@ -100,23 +106,23 @@ def get_answer(question_text): break chrome.switch_to.parent_frame() chrome.switch_to.parent_frame() - wait_time = random.randint(50, 150) + wait_time = random.randint(50, 100) wait_time = float(wait_time)/10 print("视频点已完成!冷却%.2f秒后进入章节测试!" % wait_time) + sleep(wait_time) # 完成单元测试 + wait.until(EC.presence_of_all_elements_located((By.ID, "dct2"))) chrome.find_element(By.ID, "dct2").click() chrome.switch_to.frame(chrome.find_element(By.TAG_NAME, "iframe")) chrome.switch_to.frame(chrome.find_element(By.TAG_NAME, "iframe")) chrome.switch_to.frame(chrome.find_element(By.TAG_NAME, "iframe")) - wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='ZyTop']"))) + wait.until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='ZyTop']//span"))) if chrome.find_element(By.XPATH, "//div[@class='ZyTop']//span").text == "已完成": - wait.until(EC.presence_of_all_elements_located) - chrome.back() - wait.until(EC.presence_of_all_elements_located) - chrome.back() - wait.until(EC.presence_of_all_elements_located) - chrome.back() - wait.until(EC.presence_of_all_elements_located) + chrome.switch_to.parent_frame() + chrome.switch_to.parent_frame() + chrome.switch_to.parent_frame() + wait.until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='goback']/a"))) + chrome.find_element(By.XPATH, "//div[@class='goback']/a").click() print("该章节测试已提交,不用查题!") continue workid = chrome.find_element(By.ID, "oldWorkId").get_attribute("value") @@ -152,15 +158,10 @@ def get_answer(question_text): print("该章节题目已答完!冷却%.2f秒后提交!"% wait_time) chrome.find_element(By.CLASS_NAME, 'Btn_blue_1').click() wait.until(EC.presence_of_element_located((By.XPATH, "//a[@class='bluebtn ']"))) - print(chrome.find_element(By.XPATH, "//a[@class='bluebtn ']").text) sleep(wait_time) chrome.find_element(By.XPATH, "//a[@class='bluebtn ']").click() - wait.until(EC.presence_of_all_elements_located) - chrome.back() - wait.until(EC.presence_of_all_elements_located) - chrome.back() - wait.until(EC.presence_of_all_elements_located) - chrome.back() - wait.until(EC.presence_of_all_elements_located) - -# question_resource("222994175", "eafc32b4eacf4932957b618bfb38fb63") + chrome.switch_to.parent_frame() + chrome.switch_to.parent_frame() + chrome.switch_to.parent_frame() + wait.until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='goback']/a"))) + chrome.find_element(By.XPATH, "//div[@class='goback']/a").click()