create_ad_plan.py 9.7 KB

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