send_ad.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_bottom':
  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. pass
  74. day_cost = self.driver.find_element_by_css_selector('#budget_input')
  75. day_cost.send_keys('100')
  76. self.driver.find_element_by_css_selector('#target_next_step').click()
  77. def set_ad_human(self):
  78. #TODO:设置人群
  79. pass
  80. def set_ad_idea(self):
  81. # TODO:创意创建这部分切割开
  82. # 创意创建
  83. if self.task['localtion'] == 'gzh_bottom':
  84. self.ad_idea_action.idea_pic_gzh_bottom()
  85. if self.task['localtion'] == 'gzh_movie':
  86. self.ad_idea_action.idea_movie_gzh_movie()
  87. if self.task['localtion'] == 'gzh_big_pic':
  88. self.ad_idea_action.idea_big_pic()
  89. if self.task['localtion'] == 'pyq':
  90. pass
  91. next_button = self.driver.find_element_by_class_name('adui-button-primary')
  92. WebDriverWait(self.driver, 1000).until(lambda x:(next_button.is_displayed() and next_button.is_enabled()))
  93. next_button.click()
  94. def push_ad(self):
  95. # 提交
  96. time.sleep(random.uniform(3, 4))
  97. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  98. time.sleep(random.uniform(0.5, 1))
  99. self.driver.find_element_by_class_name('adui-button-primary').click()
  100. def task_check(self):
  101. # TODO:检查任务有无输入问题,
  102. # 需要重点确认输入金额,时间是否正确.从最后页面内容进行再次确认
  103. if 'master' not in self.task.keys() or 'localtion' not in self.task.keys():
  104. exit()
  105. def run(self):
  106. # TODO:出错就截图,然后保存-----在最后一步截图,进行保存
  107. self.ad_human_action.check_human_info()
  108. self.select_ad_locations()
  109. self.set_ad_cost()
  110. self.set_ad_idea()
  111. self.push_ad()