send_ad.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 selenium.webdriver.common.keys import Keys
  9. from wechat_action.login_ad import LogIn
  10. from selenium.webdriver import ChromeOptions
  11. import time
  12. import random
  13. class SendAd():
  14. # TODO:一旦登录之后,driver就不要关闭,然后错误就部分不断重试
  15. def __init__(self, task, login_ad):
  16. self.task = task
  17. # self.task_check()
  18. self.driver = login_ad.get_driver_loged()
  19. self.ad_idea_action = IdeaAction(self.driver, task)
  20. self.ad_human_action = HumanAd(self.driver)
  21. def set_ad_title(self):
  22. title_ = self.driver.find_element_by_xpath('//*[@class="input-2lFnByGCRh"]')
  23. for i in range(40):
  24. title_.send_keys(Keys.BACKSPACE)
  25. title_.send_keys(self.task['title'])
  26. title_.send_keys(Keys.RETURN)
  27. def set_ad_locations(self):
  28. gg_button = self.driver.find_element_by_class_name(
  29. 'adui-button-hasLeftIcon')
  30. gg_button.click()
  31. # 设置广告形式
  32. self.driver.switch_to.window(self.driver.window_handles[-1])
  33. self.driver.execute_script('''
  34. window.scroll(100000,0);
  35. ''')
  36. WebDriverWait(self.driver, 10).until(
  37. lambda driver: self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT'))
  38. # 推广公众号
  39. wechat_gg = self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT')
  40. wechat_gg.click()
  41. localtion = self.task['localtion']
  42. if localtion == 'gzh_botoom':
  43. # 默认是这个选项,所以不用进行点选
  44. pass
  45. if localtion == 'gzh_movie':
  46. wechat_localtion = self.driver.find_element_by_id('pos_8')
  47. wechat_localtion.click()
  48. if localtion == 'pyq':
  49. wechat_localtion = self.driver.find_element_by_css_selector(
  50. '#wxadcontainer > div:nth-child(1) > div > div.content-2-HFBij0Uh > main > div > div:nth-child(3) > div.adui-grid-row > div:nth-child(1)')
  51. wechat_localtion.click()
  52. if localtion == 'gzh_big_pic':
  53. self.driver.find_element_by_xpath(
  54. '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[3]/div[3]/div[1]/div/button').click()
  55. time.sleep(0.5)
  56. self.driver.find_element_by_xpath(
  57. '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[3]/div[3]/div[3]/ol/li[2]').click()
  58. time.sleep(random.uniform(0.1, 0.3))
  59. # 花销设置
  60. # cost_input = self.driver.find_element_by_css_selector(
  61. # '#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')
  62. # cost_input.send_keys('100')
  63. # 进入下一页
  64. next_button = self.driver.find_element_by_css_selector('#plan_next_step')
  65. next_button.click()
  66. def set_ad_time(self):
  67. WebDriverWait(self.driver, 10).until(
  68. lambda driver: self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]'))
  69. input_elements = self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]')
  70. input_elements[0].click()
  71. input_elements[0].send_keys(Keys.RIGHT)
  72. for i in range(7):
  73. time.sleep(random.uniform(0.1, 0.2))
  74. input_elements[0].send_keys(Keys.BACKSPACE)
  75. for _ in self.task['date_list'][0][1:]:
  76. time.sleep(random.uniform(0.1, 0.2))
  77. input_elements[0].send_keys(_)
  78. time.sleep(random.uniform(0.1, 0.2))
  79. input_elements[1].click()
  80. time.sleep(0.1)
  81. input_elements[1].send_keys(Keys.RIGHT)
  82. for i in range(7):
  83. time.sleep(random.uniform(0.1, 0.2))
  84. input_elements[1].send_keys(Keys.BACKSPACE)
  85. time.sleep(random.uniform(0.1, 0.2))
  86. for _ in self.task['date_list'][1][1:]:
  87. time.sleep(random.uniform(0.1, 0.2))
  88. input_elements[1].send_keys(_)
  89. def set_ad_human_location(self):
  90. WebDriverWait(self.driver, 10).until(
  91. lambda driver: self.driver.find_element_by_css_selector('#area_input > div > i'))
  92. self.driver.find_element_by_css_selector('#area_input > div > i').click()
  93. if self.task['localtion'] == 'gzh_botoom':
  94. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  95. if self.task['localtion'] == 'gzh_movie':
  96. citys = self.driver.find_elements_by_class_name('checkbox-1pfRMRqv-R')
  97. for _ in citys:
  98. _.click()
  99. if self.task['localtion'] == 'gzh_big_pic':
  100. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  101. self.driver.find_element_by_xpath('//*[@id="bid_objective_6"]/span[1]').click()
  102. if self.task['localtion'] == 'pyq':
  103. citys = self.driver.find_elements_by_class_name('checkbox-1pfRMRqv-R')
  104. for _ in citys:
  105. _.click()
  106. def set_ad_cost(self):
  107. # 投放计划创建
  108. # 广告创建
  109. day_cost = self.driver.find_element_by_css_selector('#budget_input')
  110. WebDriverWait(self.driver, 10).until(lambda x: (day_cost.is_displayed() and day_cost.is_enabled()))
  111. day_cost.send_keys('100')
  112. self.driver.find_element_by_css_selector('#target_next_step').click()
  113. def set_ad_human(self):
  114. # TODO:检查人群是否正常
  115. self.driver.find_element_by_xpath('//*[@id="test-dmp-container"]/div[2]/span/span[1]').click()
  116. time.sleep(0.1)
  117. select_human_element = self.driver.find_element_by_xpath(
  118. '//*[@id="test-dmp-container"]/div[2]/div[2]/div[1]/div/div[1]/button/span')
  119. ActionChains(self.driver).move_to_element(select_human_element).click().perform()
  120. time.sleep(0.4)
  121. WebDriverWait(self.driver, 100).until(
  122. lambda x: [_ for _ in self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]') if
  123. _.get_attribute("placeholder") == '输入名称搜索人群包'])
  124. human_input = [_ for _ in self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]') if
  125. _.get_attribute("placeholder") == '输入名称搜索人群包'][0]
  126. for _ in self.task['human_info']:
  127. human_input.click()
  128. human_input.send_keys(_)
  129. human_input.send_keys(Keys.RETURN)
  130. time.sleep(random.uniform(0.5, 0.6))
  131. human_names = self.driver.find_elements_by_xpath('//*[@class="adui-table-cellInner"]')
  132. for human_name in human_names:
  133. if human_name.text == _:
  134. human_name.click()
  135. for _ in range(len(_) + 1):
  136. time.sleep(0.1)
  137. human_input.send_keys(Keys.BACKSPACE)
  138. self.driver.find_element_by_xpath(
  139. '//*[@class="adui-button-base adui-button-primary adui-button-small"]').click()
  140. def set_ad_idea(self):
  141. # 创意创建
  142. if self.task['localtion'] == 'gzh_botoom':
  143. self.ad_idea_action.idea_gzh_bottom()
  144. if self.task['localtion'] == 'gzh_movie':
  145. self.ad_idea_action.idea_movie_gzh_movie()
  146. if self.task['localtion'] == 'gzh_big_pic':
  147. self.ad_idea_action.idea_big_pic()
  148. if self.task['localtion'] == 'pyq':
  149. self.ad_idea_action.idea_pyq()
  150. next_button = self.driver.find_element_by_class_name('adui-button-primary')
  151. WebDriverWait(self.driver, 1000).until(lambda x: (next_button.is_displayed() and next_button.is_enabled()))
  152. next_button.click()
  153. def push_ad(self):
  154. # 提交
  155. time.sleep(random.uniform(3, 4))
  156. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  157. time.sleep(random.uniform(0.5, 1))
  158. self.driver.find_element_by_class_name('adui-button-primary').click()
  159. def task_check(self):
  160. # TODO:检查任务有无输入问题,
  161. # 需要重点确认输入金额,时间是否正确.从最后页面内容进行再次确认
  162. if 'master' not in self.task.keys() or 'localtion' not in self.task.keys():
  163. exit()
  164. def run(self):
  165. # self.ad_human_action.check_human_info()
  166. self.set_ad_locations()
  167. self.set_ad_time()
  168. self.set_ad_title()
  169. self.set_ad_human_location()
  170. self.set_ad_human()
  171. self.set_ad_cost()
  172. self.set_ad_idea()
  173. self.push_ad()