from selenium.webdriver.common.action_chains import ActionChains from selenium.common.exceptions import NoSuchElementException import time def get_new_tasks(driver): driver.get('https://www.mcbbs.net/home.php?mod=task') i = '1' while True: result = submit(driver, i) if result == 0: break elif result == -1: i = i else: result = str(result) i = result time.sleep(4) driver.get('https://www.mcbbs.net/home.php?mod=task') def submit(driver, i): try: element = driver.find_element_by_xpath("//div[@class='ptm']/table/tbody/tr[" + i + "]/td[2]/h3/a") except NoSuchElementException: return 0 else: url = element.get_attribute('href') time.sleep(2) driver.get(url) time.sleep(2) check = driver.find_element_by_xpath("//h1[@class='xs2 ptm pbm']") if check.text == '【新人任务⑧】杰出活跃': i = int(i) i = i + 1 return i button_submit = driver.find_element_by_xpath("//table[@class='tfm']/tbody/tr[3]/td[2]/a") time.sleep(2) if button_submit.get_attribute('href') == 'javascript:;': i = int(i) i = i + 1 return i else: driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") ActionChains(driver).move_to_element(button_submit).click().perform() return -1 def complete_tasks(driver): while True: try: driver.get('https://www.mcbbs.net/home.php?mod=task&item=doing') i = '1' while True: result = complete(driver, i) print(result) if result == 0: break elif result == -1: i = i else: result = str(result) i = result time.sleep(4) driver.get('https://www.mcbbs.net/home.php?mod=task&item=doing') except NoSuchElementException: continue else: break def complete(driver, i): try: element = driver.find_element_by_xpath("//div[@class='ptm']/table/tbody/tr[" + i + "]/td[2]/h3/a") except NoSuchElementException: return 0 else: url = element.get_attribute('href') time.sleep(2) driver.get(url) time.sleep(2) driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") button_submit = driver.find_element_by_xpath("//table[@class='tfm']/tbody/tr[3]/td[2]/p/a[1]/img") if button_submit.get_attribute('src') == 'https://www.mcbbs.net/static/image/task/rewardless.gif': i = int(i) i = i + 1 print('已完成') return i else: print('开始这个任务') ActionChains(driver).move_to_element(button_submit).click().perform() return -1