import pyautogui import time import os # 封装点击函数 def click(x, y, wait=2): pyautogui.click(x, y) time.sleep(wait) # 封装输入函数 def paste_text(text, wait=2): pyautogui.hotkey('ctrl', 'a') # 全选 time.sleep(wait) pyautogui.typewrite(text) time.sleep(wait) # 封装找图点击函数 - 增强异常处理 def locate_and_click(image_name, wait=4): image_path = os.path.join(desktop_path, 'yunduanxuexi', image_name) try: location = pyautogui.locateCenterOnScreen(image_path, confidence=0.8) if location: pyautogui.click(location) time.sleep(wait) return True else: print(f"未找到图片:{image_name}") return False except Exception as e: print(f"查找图片时出错:{image_name}, 错误: {str(e)}") return False # 运行程序A的主流程 def run_program_A(account, password): print(f"开始账号:{account}") try: # 打开浏览器 pyautogui.press('win') paste_text("chrome") time.sleep(2) pyautogui.press('enter') time.sleep(1) pyautogui.press('enter') time.sleep(1) pyautogui.hotkey('win', 'up') # 最大化 time.sleep(3) click(1496, 77, 0.5) paste_text("https://pc.kmelearning.com/jsncxyslhs/home/login") pyautogui.press('enter') time.sleep(2) pyautogui.press('enter') time.sleep(4) # 输入账号密码 time.sleep(1) pyautogui.press('tab') paste_text(account) time.sleep(1) pyautogui.press('tab') pyautogui.typewrite(password) time.sleep(1) pyautogui.press('tab') pyautogui.press('space') pyautogui.moveTo(1221, 600) pyautogui.dragTo(1642, 600, duration=1) time.sleep(0.5) pyautogui.press('tab') time.sleep(0.5) pyautogui.press('tab') time.sleep(0.5) pyautogui.press('tab') time.sleep(0.5) pyautogui.press('enter') time.sleep(2) # 等待页面加载 time.sleep(5) # 查找并点击01.jpg图片 found = locate_and_click("01.jpg") # ================== 关键修改 ================== # 无论是否找到图片,都执行关闭浏览器操作 pyautogui.hotkey('ctrl', 'shift', 'w') # 移到此处 time.sleep(3) if not found: print("找不到01.jpg,继续下一个账号") return # 直接返回,继续处理下一个账号 # ============================================== except Exception as e: print(f"处理账号 {account} 时出错: {str(e)}") # 确保浏览器被关闭(异常时兜底) try: pyautogui.hotkey('ctrl', 'shift', 'w') time.sleep(3) except: pass # 主逻辑不变 desktop_path = os.path.join(os.path.expanduser("~"), "Desktop") account_file_path = os.path.join(desktop_path, 'yunduanxuexi', '1.txt') try: with open(account_file_path, 'r', encoding='utf-8') as f: lines = [line.strip() for line in f.readlines() if line.strip()] for i in range(0, len(lines)-1, 2): account = lines[i] password = lines[i+1] run_program_A(account, password) except Exception as e: print(f"程序运行出错: {str(e)}")