task.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  41. ActionChains(driver).move_to_element(button_submit).click().perform()
  42. return -1
  43. def complete_tasks(driver):
  44. while True:
  45. try:
  46. driver.get('https://www.mcbbs.net/home.php?mod=task&item=doing')
  47. i = '1'
  48. while True:
  49. result = complete(driver, i)
  50. print(result)
  51. if result == 0:
  52. break
  53. elif result == -1:
  54. i = i
  55. else:
  56. result = str(result)
  57. i = result
  58. time.sleep(4)
  59. driver.get('https://www.mcbbs.net/home.php?mod=task&item=doing')
  60. except NoSuchElementException:
  61. continue
  62. else:
  63. break
  64. def complete(driver, i):
  65. try:
  66. element = driver.find_element_by_xpath("//div[@class='ptm']/table/tbody/tr[" + i + "]/td[2]/h3/a")
  67. except NoSuchElementException:
  68. return 0
  69. else:
  70. url = element.get_attribute('href')
  71. time.sleep(2)
  72. driver.get(url)
  73. time.sleep(2)
  74. driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  75. button_submit = driver.find_element_by_xpath("//table[@class='tfm']/tbody/tr[3]/td[2]/p/a[1]/img")
  76. if button_submit.get_attribute('src') == 'https://www.mcbbs.net/static/image/task/rewardless.gif':
  77. i = int(i)
  78. i = i + 1
  79. print('已完成')
  80. return i
  81. else:
  82. print('开始这个任务')
  83. ActionChains(driver).move_to_element(button_submit).click().perform()
  84. return -1