create_ad_plan.py 11 KB

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