send_ad.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from selenium import webdriver
  2. from selenium.webdriver import ActionChains
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.webdriver.support.wait import WebDriverWait
  6. from wechat_action.send_ad_idea import IdeaAction
  7. from wechat_action.human_ad import HumanAd
  8. from wechat_action.login_ad import LogIn
  9. from selenium.webdriver import ChromeOptions
  10. import time
  11. import random
  12. class SendAd():
  13. # TODO:一旦登录之后,driver就不要关闭,然后错误就部分不断重试
  14. def __init__(self, task,login_ad):
  15. self.task = task
  16. # self.task_check()
  17. self.driver = login_ad.get_driver_loged()
  18. self.ad_idea_action = IdeaAction(self.driver,task)
  19. self.ad_human_action = HumanAd(self.driver)
  20. def select_ad_locations(self):
  21. gg_button = self.driver.find_element_by_class_name(
  22. 'adui-button-hasLeftIcon')
  23. gg_button.click()
  24. # 设置广告形式
  25. self.driver.switch_to.window(self.driver.window_handles[-1])
  26. self.driver.execute_script('''
  27. window.scroll(100000,0);
  28. ''')
  29. WebDriverWait(self.driver, 10).until(
  30. lambda driver: self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT'))
  31. # 推广公众号
  32. wechat_gg = self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT')
  33. wechat_gg.click()
  34. localtion = self.task['localtion']
  35. if localtion == 'gzh_botoom':
  36. # 默认是这个选项,所以不用进行点选
  37. pass
  38. if localtion == 'gzh_movie':
  39. wechat_localtion = self.driver.find_element_by_id('pos_8')
  40. wechat_localtion.click()
  41. if localtion == 'pyq':
  42. wechat_localtion = self.driver.find_element_by_css_selector(
  43. '#wxadcontainer > div:nth-child(1) > div > div.content-2-HFBij0Uh > main > div > div:nth-child(3) > div.adui-grid-row > div:nth-child(1)')
  44. wechat_localtion.click()
  45. if localtion== 'gzh_big_pic':
  46. self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[3]/div[3]/div[1]/div/button').click()
  47. time.sleep(0.5)
  48. self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[3]/div[3]/div[3]/ol/li[2]').click()
  49. time.sleep(random.uniform(0.1, 0.3))
  50. # 花销设置
  51. # cost_input = self.driver.find_element_by_css_selector(
  52. # '#wxadcontainer > div:nth-child(1) > div > div.content-2-HFBij0Uh > main > div > div:nth-child(5) > div:nth-child(2) > div:nth-child(1) > div > input')
  53. # cost_input.send_keys('100')
  54. # 进入下一页
  55. next_button = self.driver.find_element_by_css_selector('#plan_next_step')
  56. next_button.click()
  57. def set_ad_cost(self):
  58. # 投放计划创建
  59. # 广告创建
  60. WebDriverWait(self.driver, 10).until(
  61. lambda driver: self.driver.find_element_by_css_selector('#area_input > div > i'))
  62. self.driver.find_element_by_css_selector('#area_input > div > i').click()
  63. if self.task['localtion'] == 'gzh_botoom':
  64. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  65. if self.task['localtion'] == 'gzh_movie':
  66. citys = self.driver.find_elements_by_class_name('checkbox-1pfRMRqv-R')
  67. for _ in citys:
  68. _.click()
  69. if self.task['localtion']=='gzh_big_pic':
  70. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  71. self.driver.find_element_by_xpath('//*[@id="bid_objective_6"]/span[1]').click()
  72. if self.task['localtion'] == 'pyq':
  73. citys = self.driver.find_elements_by_class_name('checkbox-1pfRMRqv-R')
  74. for _ in citys:
  75. _.click()
  76. day_cost = self.driver.find_element_by_css_selector('#budget_input')
  77. #TODO: 投放时间,投放位置,投放价格还没有进行设置
  78. WebDriverWait(self.driver, 10).until(lambda x:(day_cost.is_displayed() and day_cost.is_enabled()))
  79. day_cost.send_keys('100')
  80. self.driver.find_element_by_css_selector('#target_next_step').click()
  81. def set_ad_human(self):
  82. #TODO:设置人群
  83. #TODO:检查人群是否正常
  84. pass
  85. def set_ad_idea(self):
  86. # 创意创建
  87. if self.task['localtion'] == 'gzh_botoom':
  88. self.ad_idea_action.idea_gzh_bottom()
  89. if self.task['localtion'] == 'gzh_movie':
  90. self.ad_idea_action.idea_movie_gzh_movie()
  91. if self.task['localtion'] == 'gzh_big_pic':
  92. self.ad_idea_action.idea_big_pic()
  93. if self.task['localtion'] == 'pyq':
  94. self.ad_idea_action.idea_pyq()
  95. next_button = self.driver.find_element_by_class_name('adui-button-primary')
  96. WebDriverWait(self.driver, 1000).until(lambda x:(next_button.is_displayed() and next_button.is_enabled()))
  97. next_button.click()
  98. def push_ad(self):
  99. # 提交
  100. time.sleep(random.uniform(3, 4))
  101. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  102. time.sleep(random.uniform(0.5, 1))
  103. self.driver.find_element_by_class_name('adui-button-primary').click()
  104. def task_check(self):
  105. # TODO:检查任务有无输入问题,
  106. # 需要重点确认输入金额,时间是否正确.从最后页面内容进行再次确认
  107. if 'master' not in self.task.keys() or 'localtion' not in self.task.keys():
  108. exit()
  109. def run(self):
  110. # TODO:出错就截图,然后保存-----在最后一步截图,进行保存
  111. self.ad_human_action.check_human_info()
  112. self.select_ad_locations()
  113. self.set_ad_cost()
  114. self.set_ad_idea()
  115. self.push_ad()