task.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from selenium.webdriver.common.action_chains import ActionChains
  2. from selenium.common.exceptions import NoSuchElementException
  3. import time
  4. def get_new_tasks(driver):
  5. driver.get('https://www.mcbbs.net/home.php?mod=task')
  6. i = '1'
  7. while True:
  8. result = submit(driver, i)
  9. if result == 0:
  10. break
  11. elif result == -1:
  12. i = i
  13. else:
  14. result = str(result)
  15. i = result
  16. time.sleep(4)
  17. driver.get('https://www.mcbbs.net/home.php?mod=task')
  18. def submit(driver, i):
  19. try:
  20. element = driver.find_element_by_xpath("//div[@class='ptm']/table/tbody/tr[" + i + "]/td[2]/h3/a")
  21. except NoSuchElementException:
  22. return 0
  23. else:
  24. url = element.get_attribute('href')
  25. time.sleep(2)
  26. driver.get(url)
  27. time.sleep(2)
  28. check = driver.find_element_by_xpath("//h1[@class='xs2 ptm pbm']")
  29. if check.text == '【新人任务⑧】杰出活跃':
  30. i = int(i)
  31. i = i + 1
  32. return i
  33. button_submit = driver.find_element_by_xpath("//table[@class='tfm']/tbody/tr[3]/td[2]/a")
  34. time.sleep(2)
  35. if button_submit.get_attribute('href') == 'javascript:;':
  36. i = int(i)
  37. i = i + 1
  38. return i
  39. else:
  40. ActionChains(driver).move_to_element(button_submit).click().perform()
  41. return -1
  42. def complete_tasks(driver):
  43. driver.get('https://www.mcbbs.net/home.php?mod=task&item=doing')
  44. i = '1'
  45. while True:
  46. result = complete(driver, i)
  47. print(result)
  48. if result == 0:
  49. break
  50. elif result == -1:
  51. i = i
  52. else:
  53. result = str(result)
  54. i = result
  55. time.sleep(4)
  56. driver.get('https://www.mcbbs.net/home.php?mod=task&item=doing')
  57. def complete(driver, i):
  58. try:
  59. element = driver.find_element_by_xpath("//div[@class='ptm']/table/tbody/tr[" + i + "]/td[2]/h3/a")
  60. except NoSuchElementException:
  61. return 0
  62. else:
  63. url = element.get_attribute('href')
  64. time.sleep(2)
  65. driver.get(url)
  66. time.sleep(2)
  67. button_submit = driver.find_element_by_xpath("//table[@class='tfm']/tbody/tr[3]/td[2]/p/a[1]/img")
  68. if button_submit.get_attribute('src') == 'https://www.mcbbs.net/static/image/task/rewardless.gif':
  69. i = int(i)
  70. i = i + 1
  71. print('已完成')
  72. return i
  73. else:
  74. print('开始这个任务')
  75. ActionChains(driver).move_to_element(button_submit).click().perform()
  76. return -1