create_ad_plan.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 logging
  14. import random
  15. class CreateAdPlan():
  16. # TODO:因为人群包现在可能存在唯一性,公众号间的复制,需要怎么处理
  17. def __init__(self, task, login_ad):
  18. self.task = task
  19. # self.task_check()
  20. self.login_ad = login_ad
  21. self.driver = login_ad.get_driver_loged()
  22. self.ad_idea_action = IdeaAction(self.driver, task)
  23. self.ad_human_action = HumanAd(self.driver)
  24. def set_ad_title(self):
  25. # TODO:广告和投放计划,都需要修改名字,修改成一个名字
  26. logging.info('开始设置广告标题')
  27. title_ = self.driver.find_element_by_xpath('//*[@class="input-2lFnByGCRh"]')
  28. for i in range(40):
  29. title_.send_keys(Keys.BACKSPACE)
  30. title_.click()
  31. time.sleep(0.1)
  32. title_.send_keys(self.task['title'])
  33. title_.send_keys(Keys.RETURN)
  34. time.sleep(random.uniform(0.1, 0.2))
  35. def set_ad_cpm(self):
  36. # TODO:设置完之后,test函数中进行对应修改
  37. logging.info('开始设置cpm')
  38. if self.task['cpm']['cpm_set'] == 2:
  39. self.driver.save_screenshot('cpm{}.png'.format(time.time()))
  40. self.driver.find_element_by_xpath(
  41. '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[2]/div[1]/div/div[2]/div/div[1]/section[2]/form/div/div[1]/div[1]/button[2]/span').click()
  42. time.sleep(0.1)
  43. select_elements = self.driver.find_elements_by_xpath('//*[@class="adui-radio-base adui-radio-small"]')
  44. for _ in select_elements:
  45. if '优先跑量' in _.text and self.task['cpm']['module'] == 2:
  46. _.click()
  47. time.sleep(0.1)
  48. if '下单' in _.text:
  49. _.click()
  50. time.sleep(0.1)
  51. def set_ad_locations(self):
  52. logging.info('设置对应广告位')
  53. gg_button = self.driver.find_element_by_class_name(
  54. 'adui-button-hasLeftIcon')
  55. gg_button.click()
  56. # 设置广告形式
  57. self.driver.switch_to.window(self.driver.window_handles[-1])
  58. self.driver.execute_script('''
  59. window.scroll(100000,0);
  60. ''')
  61. WebDriverWait(self.driver, 10).until(
  62. lambda driver: self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT'))
  63. # 推广公众号
  64. wechat_gg = self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT')
  65. wechat_gg.click()
  66. localtion = self.task['localtion']
  67. if localtion == 'gzh_botoom':
  68. # 默认是这个选项,所以不用进行点选
  69. pass
  70. if localtion == 'gzh_movie':
  71. wechat_localtion = self.driver.find_element_by_id('pos_8')
  72. wechat_localtion.click()
  73. if localtion == 'pyq':
  74. wechat_localtion = self.driver.find_element_by_css_selector(
  75. '#wxadcontainer > div:nth-child(1) > div > div.content-2-HFBij0Uh > main > div > div:nth-child(3) > div.adui-grid-row > div:nth-child(1)')
  76. wechat_localtion.click()
  77. if localtion == 'gzh_big_pic':
  78. self.driver.find_element_by_xpath(
  79. '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[3]/div[3]/div[1]/div/button').click()
  80. time.sleep(0.5)
  81. self.driver.find_element_by_xpath(
  82. '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[3]/div[3]/div[3]/ol/li[2]').click()
  83. time.sleep(random.uniform(0.1, 0.3))
  84. # 花销设置
  85. # cost_input = self.driver.find_element_by_css_selector(
  86. # '#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')
  87. # cost_input.send_keys('100')
  88. # 进入下一页
  89. next_button = self.driver.find_element_by_css_selector('#plan_next_step')
  90. next_button.click()
  91. def set_ad_time(self):
  92. logging.info('设置对应投放时间')
  93. WebDriverWait(self.driver, 10).until(
  94. lambda driver: self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]'))
  95. input_elements = self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]')
  96. if self.task['date_set']['date_module'] == 2:
  97. select_elements = self.driver.find_elements_by_xpath('//*[@class="adui-radio-base adui-radio-small"]')
  98. for _ in select_elements:
  99. if '长期投放' in _.text:
  100. _.click()
  101. input_elements[0].click()
  102. input_elements[0].send_keys(Keys.RIGHT)
  103. for i in range(7):
  104. time.sleep(random.uniform(0.1, 0.2))
  105. input_elements[0].send_keys(Keys.BACKSPACE)
  106. for _ in self.task['date_set']['date_list'][0][1:]:
  107. time.sleep(random.uniform(0.1, 0.2))
  108. input_elements[0].send_keys(_)
  109. time.sleep(random.uniform(0.1, 0.2))
  110. else:
  111. input_elements[1].click()
  112. time.sleep(0.1)
  113. input_elements[1].send_keys(Keys.RIGHT)
  114. for i in range(7):
  115. time.sleep(random.uniform(0.1, 0.2))
  116. input_elements[1].send_keys(Keys.BACKSPACE)
  117. time.sleep(random.uniform(0.1, 0.2))
  118. for _ in self.task['date_set']['date_list'][1][1:]:
  119. time.sleep(random.uniform(0.1, 0.2))
  120. input_elements[1].send_keys(_)
  121. self.driver.save_screenshot('time-{}.png'.format(time.time()))
  122. # self.driver.find_element_by_xpath(
  123. # '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[2]/div[1]/div/div[2]/div/div[1]/section[1]/div[2]/form/div[2]/div/div/div/svg').click()
  124. time.sleep(0.1)
  125. def set_ad_human_location(self):
  126. logging.info('设置对应人群信息-非人群包')
  127. WebDriverWait(self.driver, 10).until(
  128. lambda driver: self.driver.find_element_by_css_selector('#area_input > div > i'))
  129. self.driver.find_element_by_css_selector('#area_input > div > i').click()
  130. if self.task['localtion'] == 'gzh_botoom':
  131. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  132. if self.task['localtion'] == 'gzh_movie':
  133. citys = self.driver.find_elements_by_class_name('checkbox-1pfRMRqv-R')
  134. for _ in citys:
  135. _.click()
  136. if self.task['localtion'] == 'gzh_big_pic':
  137. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  138. self.driver.find_element_by_xpath('//*[@id="bid_objective_6"]/span[1]').click()
  139. if self.task['localtion'] == 'pyq':
  140. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  141. def set_ad_cost(self):
  142. # 投放计划创建
  143. # 广告创建
  144. logging.info('设置对应广告花销')
  145. day_cost = self.driver.find_element_by_css_selector('#budget_input')
  146. WebDriverWait(self.driver, 10).until(lambda x: (day_cost.is_displayed() and day_cost.is_enabled()))
  147. day_cost.send_keys('101')
  148. time.sleep(5)
  149. self.driver.find_element_by_css_selector('#target_next_step').click()
  150. self.driver.save_screenshot('click{}.png'.format(time.time()))
  151. time_element = self.driver.find_element_by_xpath('//*[@class="title-1CSarR7XgH"]')
  152. ActionChains(self.driver).move_to_element(time_element).perform()
  153. time.sleep(10)
  154. self.driver.save_screenshot('click-2-{}.png'.format(time.time()))
  155. for i in range(4):
  156. if len(self.driver.find_elements_by_css_selector('#target_next_step')) == 0:
  157. break
  158. self.driver.find_element_by_css_selector('#target_next_step').click()
  159. time.sleep(5)
  160. ActionChains(self.driver).move_to_element(time_element).perform()
  161. self.driver.save_screenshot('click-{}-{}.png'.format((2 + i), time.time()))
  162. def set_ad_human(self):
  163. # TODO:检查人群是否正常
  164. logging.info('设置人群包')
  165. self.driver.find_element_by_xpath('//*[@id="test-dmp-container"]/div[2]/span/span[1]').click()
  166. time.sleep(0.1)
  167. select_human_element = self.driver.find_element_by_xpath(
  168. '//*[@id="test-dmp-container"]/div[2]/div[2]/div[1]/div/div[1]/button/span')
  169. ActionChains(self.driver).move_to_element(select_human_element).click().perform()
  170. time.sleep(0.4)
  171. WebDriverWait(self.driver, 100).until(
  172. lambda x: [_ for _ in self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]') if
  173. _.get_attribute("placeholder") == '输入名称搜索人群包'])
  174. human_input = [_ for _ in self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]') if
  175. _.get_attribute("placeholder") == '输入名称搜索人群包'][0]
  176. for _ in self.task['human_info_package']:
  177. human_input.click()
  178. human_input.send_keys(_)
  179. human_input.send_keys(Keys.RETURN)
  180. time.sleep(random.uniform(1, 1.2))
  181. human_names = self.driver.find_elements_by_xpath('//*[@class="adui-table-cellInner"]')
  182. for human_name in human_names:
  183. if human_name.text == _:
  184. human_name.click()
  185. for _ in range(len(_) + 1):
  186. time.sleep(0.1)
  187. human_input.send_keys(Keys.BACKSPACE)
  188. self.driver.find_element_by_xpath(
  189. '//*[@class="adui-button-base adui-button-primary adui-button-small"]').click()
  190. def set_ad_age(self):
  191. age_elements = self.driver.find_elements_by_xpath('//*[@class="adui-select-selector"]')
  192. age_elements[0].click()
  193. time.sleep(0.1)
  194. # 256,一截,[14(17),24][25,32][33,40][41,48][49,56][57,64]
  195. if self.task['age'][0] == 14:
  196. age_elements[1].click()
  197. else:
  198. self.driver.execute_script('''
  199. document.getElementsByClassName('rc-virtual-list-holder')[0].scrollTop={};
  200. '''.format(self.task['age'][0] - 17 / 7) * 256)
  201. time.sleep(random.uniform(0.1, 0.2))
  202. self.driver.find_element_by_xpath('//*[@id="begin_age_{}"]'.format(int(self.task['age'][0]))).click()
  203. # 年龄---设置
  204. age_elements[1].click()
  205. self.driver.execute_script('''
  206. document.getElementsByClassName('rc-virtual-list-holder')[0].scrollTop={};
  207. '''.format(self.task['age'][0] - 17 / 7) * 256)
  208. def set_ad_idea(self):
  209. # 创意创建
  210. logging.info('设置创意')
  211. if self.task['localtion'] == 'gzh_botoom':
  212. self.ad_idea_action.idea_gzh_bottom()
  213. if self.task['localtion'] == 'gzh_movie':
  214. self.ad_idea_action.idea_movie_gzh_movie()
  215. if self.task['localtion'] == 'gzh_big_pic':
  216. self.ad_idea_action.idea_big_pic()
  217. if self.task['localtion'] == 'pyq':
  218. self.ad_idea_action.idea_pyq()
  219. next_button = self.driver.find_element_by_class_name('adui-button-primary')
  220. WebDriverWait(self.driver, 1000).until(lambda x: (next_button.is_displayed() and next_button.is_enabled()))
  221. next_button.click()
  222. def push_ad(self):
  223. # 提交
  224. logging.info('提交广告')
  225. time.sleep(random.uniform(3, 4))
  226. agreement_element = self.driver.find_element_by_class_name('label-3_CrbQVNtc')
  227. if agreement_element.get_attribute('aria-checked') != 'true':
  228. self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
  229. time.sleep(random.uniform(0.5, 1))
  230. self.driver.find_element_by_class_name('adui-button-primary').click()
  231. WebDriverWait(self.driver, 10).until(
  232. lambda driver: True if len(
  233. re.findall('广告已提交审核', self.driver.page_source)) else False)
  234. def task_check(self):
  235. # TODO:
  236. # 1.检查任务有无输入问题,
  237. # 2.需要重点确认输入金额,时间是否正确.从最后页面内容进行再次确认
  238. # if 'master' not in self.task.keys() or 'localtion' not in self.task.keys():
  239. # exit()
  240. pass
  241. def check_is_sucess(self, plan_name):
  242. logging.info('检查广告计划创建是否成功')
  243. # TODO:进一步修改
  244. # 进入搜索页面,
  245. click_elements = self.driver.find_elements_by_xpath('//*[contains(@class, "navItem-3MjWMCbkWT")]/span')
  246. for _ in click_elements:
  247. if '广告' in _.text:
  248. _.click()
  249. time.sleep(random.uniform(0.1, 0.2))
  250. # 设置状态为--审核中
  251. choice_elements = self.driver.find_elements_by_xpath('//*[contains(@class, "tag-3loTlkncVq")]/span')
  252. for _ in choice_elements:
  253. if '状态' in _.text:
  254. _.click()
  255. time.sleep(random.uniform(0.1, 0.2))
  256. lable_elements = self.driver.find_elements_by_xpath('//*[contains(@class, "label-2ZOAbuO31o")]/span')
  257. for _ in lable_elements:
  258. if '审核中' in _.text:
  259. _.click()
  260. # 输入搜索字段
  261. input_element = self.driver.find_element_by_xpath('//*[contains(@class, "search-2yiDvyFHoH")]/span')
  262. input_element.click()
  263. time.sleep(random.uniform(0.1, 0.2))
  264. input_element.send_keys()
  265. # 查看有无对应数据
  266. def run(self, err_num=0):
  267. try:
  268. # self.ad_human_action.check_human_info()
  269. self.set_ad_locations()
  270. self.set_ad_time()
  271. self.set_ad_title()
  272. self.set_ad_cpm()
  273. self.set_ad_human_location()
  274. self.set_ad_human()
  275. self.set_ad_cost()
  276. self.set_ad_idea()
  277. self.push_ad()
  278. # 切回到前一页
  279. self.driver.switch_to.window(self.driver.window_handles[-1])
  280. logging.info('创建广告计划成功')
  281. return {'sucess': True, 'result_info': ''}
  282. except Exception as e:
  283. time.sleep(5)
  284. self.driver.save_screenshot('liuyi{}.png'.format(time.time()))
  285. time.sleep(5)
  286. raise
  287. # TODO:有空时讲 e 内容设置为原始内容
  288. if err_num > 3:
  289. return {'sucess': False, 'result_info': str(e)}
  290. else:
  291. return self.run(err_num=err_num + 1)
  292. pass