from selenium.common.exceptions import NoSuchElementException import json import time def login(driver, cookie_path): driver.get('https://www.mcbbs.net/forum-chat-1.html') driver.delete_all_cookies() with open(cookie_path, 'r') as cookief: cookieslist = json.load(cookief) for cookie in cookieslist: driver.add_cookie(cookie) driver.get('https://www.mcbbs.net/home.php?mod=spacecp') time.sleep(2) try: element = driver.find_element_by_xpath("//div[@class='alert_info']/p") except NoSuchElementException: print('已登录') else: if '需要先登录' in element.text: return False else: return True try: element = driver.find_element_by_xpath("//div[@class='alert_error']") except NoSuchElementException: return True else: if '空间已被锁定无法访问' in element.text: print('被禁言') return False else: return True