create_ad.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. from wechat_action.login_ad import LogIn
  2. from selenium import webdriver
  3. from selenium.webdriver import ActionChains
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.wait import WebDriverWait
  7. from selenium.webdriver import ChromeOptions
  8. from selenium.webdriver.common.keys import Keys
  9. import logging
  10. import time
  11. import random
  12. import re
  13. import os
  14. from logging import handlers
  15. class CreateAd:
  16. def __init__(self, service_name, wechat_name):
  17. # TODO:创建完页面需要关闭所有页面并回到一开始的页面
  18. self.driver = LogIn(service_name, wechat_name).get_driver_loged()
  19. self.get_into_create_page()
  20. self.send_file_limit_num = 8
  21. def get_into_create_page(self):
  22. # 进入创建页面
  23. self.driver.find_element_by_id('material').click()
  24. WebDriverWait(self.driver, 10).until(lambda driver: driver.find_element_by_class_name('ui-fl-r'))
  25. time.sleep(random.uniform(0.1, 0.2))
  26. self.driver.find_element_by_class_name('ui-fl-r').click()
  27. WebDriverWait(self.driver, 5).until(lambda driver: driver.find_element_by_css_selector(
  28. '#wxadcontainer > div:nth-child(1) > div:nth-child(2) > div.dialog-1fj_N480ZT > div > div > div:nth-child(1) > div.dialogCardFooter-17KpBD1lgN > button'))
  29. self.driver.find_element_by_css_selector(
  30. '#wxadcontainer > div:nth-child(1) > div:nth-child(2) > div.dialog-1fj_N480ZT > div > div > div:nth-child(1) > div.dialogCardFooter-17KpBD1lgN > button').click()
  31. self.driver.switch_to.window(self.driver.window_handles[-1])
  32. WebDriverWait(self.driver, 100).until(lambda driver: driver.find_element_by_class_name('addContent-8pexaaAGYy'))
  33. self.driver.find_element_by_class_name('addContent-8pexaaAGYy').click()
  34. WebDriverWait(self.driver, 100).until(lambda driver: driver.find_element_by_class_name('topArea-qOwEAeNuIn'))
  35. def create_layout(self):
  36. # 数据库获取数据,然后进行编排
  37. # 创建编排
  38. pass
  39. def set_advertisement_sign(self):
  40. # 设置广告标记
  41. now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  42. self.driver.find_element_by_xpath('//*[@class="icon-edQB0KK2VG"]').click()
  43. input_element = self.driver.find_element_by_xpath('//input[@class="input-2lFnByGCRh"]')
  44. input_element.send_keys(Keys.BACKSPACE)
  45. input_element.send_keys(now_time)
  46. return now_time
  47. def set_background_color(self):
  48. color_buttons = self.driver.find_elements_by_class_name('adui-cp-picker')
  49. c_buttons_can_use = []
  50. for _ in color_buttons:
  51. if _.is_displayed() and _.is_enabled():
  52. c_buttons_can_use.append(_)
  53. c_buttons_can_use[0].click()
  54. time.sleep(random.uniform(0.2, 0.3))
  55. input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  56. for _ in input_elements:
  57. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == 'FFFFFF':
  58. _.click()
  59. _.send_keys('F7F7F7')
  60. def set_head_assemb(self, info):
  61. def single_page_set():
  62. self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/div[1]/div/div[1]/div/div').click()
  63. time.sleep(random.uniform(0.1, 0.2))
  64. file_name = os.path.basename(info_v)
  65. self.driver.find_element_by_class_name('upload-img-item-inner-2gsg7NjaZ8').click()
  66. WebDriverWait(self.driver, 10).until(
  67. lambda x: len([_ for _ in self.driver.find_elements_by_class_name('title-29sncpKgTl') if
  68. _.is_displayed() and _.is_enabled()]) > 0)
  69. turn_page_buttons = self.driver.find_elements_by_class_name('paginationIcon-1EfoH0sNRF')
  70. can_use_button = []
  71. for _ in turn_page_buttons:
  72. if _.is_enabled() and _.is_displayed():
  73. can_use_button.append(_)
  74. # 不断翻页获取到元素为止
  75. chose_over = False
  76. while True:
  77. page_elements = self.driver.find_elements_by_class_name('title-29sncpKgTl')
  78. for _ in page_elements:
  79. if _.is_displayed() and _.is_enabled():
  80. if file_name in _.text:
  81. _.click()
  82. chose_over = True
  83. break
  84. if chose_over or len(can_use_button) == 0:
  85. break
  86. # 翻到最后一页时停止
  87. page_text = self.driver.find_element_by_class_name('count_small-37CcvfzoTl').text
  88. page_nums = re.findall('\d+', page_text)
  89. if int(page_nums[0].strip()) == int(page_nums[1].strip()):
  90. break
  91. # 翻页
  92. can_use_button[2].click()
  93. # 确保翻页成功
  94. WebDriverWait(self.driver, 10).until(
  95. lambda x: page_text != self.driver.find_element_by_class_name('count_small-37CcvfzoTl').text)
  96. self.driver.find_element_by_xpath('//*[@id="test_material_container_confirm"]').click()
  97. try:
  98. WebDriverWait(self.driver, 4).until(
  99. lambda driver: driver.find_element_by_class_name(
  100. 'btnFist-uueBS6DQFa'))
  101. _ = self.driver.find_element_by_class_name(
  102. 'btnFist-uueBS6DQFa')
  103. WebDriverWait(self.driver, 4).until(
  104. lambda x: (_.is_displayed() and _.is_enabled()))
  105. time.sleep(1)
  106. _.click()
  107. WebDriverWait(self.driver, 100).until(
  108. lambda driver: driver.find_element_by_class_name(
  109. 'btn-3E823IXt3m'))
  110. _ = self.driver.find_element_by_class_name(
  111. 'btn-3E823IXt3m')
  112. WebDriverWait(self.driver, 100).until(
  113. lambda x: (_.is_displayed() and _.is_enabled()))
  114. time.sleep(1)
  115. _.click()
  116. except Exception as e:
  117. pass
  118. logging.info('头版首页单图上传结束')
  119. def multi_page_set():
  120. self.driver.find_element_by_css_selector(
  121. '#stage-sidebar > div.topArea-qOwEAeNuIn > div > div:nth-child(2)').click()
  122. time.sleep(random.uniform(0.1, 0.2))
  123. page_size = len(info_v)
  124. if page_size == 3:
  125. pass
  126. if page_size == 4:
  127. self.driver.find_element_by_css_selector(
  128. '#stage-settings > div:nth-child(1) > div > div > div:nth-child(3) > div.adui-form-item > div.adui-form-control > div > button:nth-child(2)').click()
  129. if page_size == 6:
  130. self.driver.find_element_by_css_selector(
  131. '#stage-settings > div:nth-child(1) > div > div > div:nth-child(3) > div.adui-form-item > div.adui-form-control > div > button:nth-child(3)').click()
  132. time.sleep(random.uniform(0.1, 0.2))
  133. self.driver.find_element_by_class_name('imageUploadItem-tA9JX0RWua').click()
  134. WebDriverWait(self.driver, 1000).until(
  135. lambda driver: self.driver.find_element_by_class_name('adui-tabs-tab')
  136. )
  137. input_elements = self.driver.find_elements_by_tag_name('input')
  138. input_find_element = None
  139. for _ in input_elements:
  140. if '输入关键词搜索素材' in _.get_attribute('placeholder'):
  141. input_find_element = _
  142. for _ in info_v:
  143. file_name = os.path.basename(_)
  144. logging.info(file_name)
  145. input_find_element.send_keys(file_name)
  146. input_find_element.send_keys(Keys.RETURN)
  147. WebDriverWait(self.driver, 1000).until(
  148. lambda driver: driver.find_element_by_class_name('img-2HvhMmpnzP'))
  149. file_element = self.driver.find_element_by_class_name('img-2HvhMmpnzP')
  150. WebDriverWait(self.driver, 1000).until(
  151. lambda x: (file_element.is_displayed() and file_element.is_enabled()))
  152. ActionChains(self.driver).move_to_element(file_element).perform()
  153. time.sleep(random.uniform(1, 2))
  154. file_element.click()
  155. for i in range(len(file_name) + 10):
  156. input_find_element.send_keys(Keys.BACKSPACE)
  157. time.sleep(random.uniform(1, 4))
  158. self.driver.find_element_by_xpath('/html/body/div[12]/div/div/div[2]/div/div[3]/button[2]').click()
  159. time.sleep(random.uniform(2, 4))
  160. # 切图操作如果有的话进行对应操作.
  161. for i in range(len(info_v) + 1):
  162. try:
  163. WebDriverWait(self.driver, 4).until(
  164. lambda driver: driver.find_element_by_xpath(
  165. '/html/body/div[13]/div/div/div[2]/div/div[3]/button[2]'))
  166. self.driver.find_element_by_xpath('/html/body/div[13]/div/div/div[2]/div/div[3]/button[2]').click()
  167. except:
  168. pass
  169. logging.info('头版多图选择 结束')
  170. def movie_set():
  171. file_name = os.path.basename(info_v)
  172. self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/div[1]/div/div[3]/div/div').click()
  173. self.driver.find_element_by_xpath('//*[@class="comptEditButton-2JsnAFdOGZ"]').click()
  174. WebDriverWait(self.driver, 100).until(
  175. lambda x: len([_ for _ in self.driver.find_elements_by_class_name('title-29sncpKgTl') if
  176. _.is_displayed() and _.is_enabled()]) > 0)
  177. #
  178. turn_page_buttons = self.driver.find_elements_by_class_name('paginationIcon-1EfoH0sNRF')
  179. can_use_button = []
  180. for _ in turn_page_buttons:
  181. if _.is_enabled() and _.is_displayed():
  182. can_use_button.append(_)
  183. # 不断翻页获取到元素为止
  184. chose_over = False
  185. while True:
  186. page_elements = self.driver.find_elements_by_class_name('title-29sncpKgTl')
  187. for _ in page_elements:
  188. if _.is_displayed() and _.is_enabled():
  189. if file_name in _.text:
  190. _.click()
  191. chose_over = True
  192. break
  193. if chose_over or len(can_use_button) == 0:
  194. # can_use_button 数量说明是否可以翻页,如果不可以翻页,则直接停止
  195. break
  196. # 翻到最后一页时停止
  197. page_text_elements = self.driver.find_elements_by_class_name('count_small-37CcvfzoTl')
  198. for _ in page_text_elements:
  199. if _.is_enabled() and _.is_displayed():
  200. page_text = _.text
  201. page_text_element = _
  202. page_nums = re.findall('\d+', page_text)
  203. if int(page_nums[0].strip()) == int(page_nums[1].strip()):
  204. break
  205. # 翻页
  206. can_use_button[2].click()
  207. # 确保翻页成功
  208. WebDriverWait(self.driver, 10).until(
  209. lambda x: page_text != page_text_element.text)
  210. sc_buttons = self.driver.find_elements_by_id('test_material_container_confirm')
  211. for _ in sc_buttons:
  212. if _.is_enabled() and _.is_displayed():
  213. _.click()
  214. info_key = list(info.keys())[0]
  215. info_v = info[info_key]
  216. if info_key == 'page':
  217. single_page_set()
  218. if info_key == 'multi_page':
  219. multi_page_set()
  220. if info_key == 'movie':
  221. movie_set()
  222. # 设置头部组件
  223. pass
  224. def set_page(self, page_path):
  225. # 设置图片模块
  226. self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/section/div[2]/div[1]/div/div').click()
  227. time.sleep(random.uniform(0.1, 0.2))
  228. file_name = os.path.basename(page_path)
  229. upload_elements = self.driver.find_elements_by_class_name('upload-img-item-inner-2gsg7NjaZ8')
  230. for _ in upload_elements:
  231. if _.is_displayed() and _.is_enabled():
  232. _.click()
  233. WebDriverWait(self.driver, 10).until(
  234. lambda x: len([_ for _ in self.driver.find_elements_by_class_name('title-29sncpKgTl') if
  235. _.is_displayed() and _.is_enabled()]) > 0)
  236. turn_page_buttons = self.driver.find_elements_by_class_name('paginationIcon-1EfoH0sNRF')
  237. can_use_button = []
  238. for _ in turn_page_buttons:
  239. if _.is_enabled() and _.is_displayed():
  240. can_use_button.append(_)
  241. # 不断翻页获取到元素为止
  242. chose_over = False
  243. while True:
  244. page_elements = self.driver.find_elements_by_class_name('title-29sncpKgTl')
  245. for _ in page_elements:
  246. if _.is_displayed() and _.is_enabled():
  247. if file_name in _.text:
  248. _.click()
  249. chose_over = True
  250. break
  251. if chose_over or len(can_use_button) == 0:
  252. # can_use_button 数量说明是否可以翻页,如果不可以翻页,则直接停止
  253. break
  254. # 翻到最后一页时停止
  255. page_text_elements = self.driver.find_elements_by_class_name('count_small-37CcvfzoTl')
  256. for _ in page_text_elements:
  257. if _.is_enabled() and _.is_displayed():
  258. page_text = _.text
  259. page_text_element = _
  260. page_nums = re.findall('\d+', page_text)
  261. if int(page_nums[0].strip()) == int(page_nums[1].strip()):
  262. break
  263. # 翻页
  264. can_use_button[2].click()
  265. # 确保翻页成功
  266. WebDriverWait(self.driver, 10).until(
  267. lambda x: page_text != page_text_element.text)
  268. sc_buttons = self.driver.find_elements_by_id('test_material_container_confirm')
  269. for _ in sc_buttons:
  270. if _.is_enabled() and _.is_displayed():
  271. _.click()
  272. try:
  273. WebDriverWait(self.driver, 4).until(
  274. lambda driver: driver.find_element_by_class_name(
  275. 'btnFist-uueBS6DQFa'))
  276. _ = self.driver.find_element_by_class_name(
  277. 'btnFist-uueBS6DQFa')
  278. WebDriverWait(self.driver, 4).until(
  279. lambda x: (_.is_displayed() and _.is_enabled()))
  280. # 已经出现但任需要等待
  281. time.sleep(1)
  282. _.click()
  283. WebDriverWait(self.driver, 100).until(
  284. lambda x: len([_ for _ in self.driver.find_elements_by_class_name('btn-3E823IXt3m') if
  285. _.is_displayed() and _.is_enabled()]) > 0)
  286. for _ in self.driver.find_elements_by_class_name('btn-3E823IXt3m'):
  287. if _.is_displayed() and _.is_enabled():
  288. _.click()
  289. except Exception as e:
  290. logging.info(e)
  291. logging.info('图片模块设置结束')
  292. def set_content(self, config_info):
  293. # 设置文本模块
  294. # 1.文本内容2.文本颜色3.是否加粗4.文本位置设置5.文本大小
  295. # 设置文本内容
  296. self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/section/div[2]/div[5]/div/div').click()
  297. time.sleep(random.uniform(0.2, 0.3))
  298. input_elements = self.driver.find_elements_by_xpath('//textarea[@class="adui-input-base"]')
  299. for _ in input_elements:
  300. if _.is_enabled() and _.is_displayed():
  301. _.send_keys(config_info['content'])
  302. # 设置颜色
  303. if config_info['color'] != '595959':
  304. color_buttons = self.driver.find_elements_by_class_name('adui-cp-picker')
  305. c_buttons_can_use = []
  306. for _ in color_buttons:
  307. if _.is_displayed() and _.is_enabled():
  308. c_buttons_can_use.append(_)
  309. c_buttons_can_use[1].click()
  310. time.sleep(random.uniform(0.2, 0.3))
  311. input_elements = self.driver.find_elements_by_tag_name('input')
  312. for _ in input_elements:
  313. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '595959':
  314. _.click()
  315. _.send_keys(config_info['color'])
  316. # 设置是否加粗
  317. if config_info['is_bold']:
  318. big_elements = self.driver.find_elements_by_class_name('adui-button-content')
  319. for _ in big_elements:
  320. if _.text == '加粗' and _.is_displayed() and _.is_enabled():
  321. _.click()
  322. # 设置文本位置
  323. if config_info['loc'] != 'left':
  324. loc_buttons = self.driver.find_elements_by_xpath(
  325. "//div[contains(@class, 'adui-button-group_banner')]//button")
  326. loc_buttons_can_use = []
  327. for _ in loc_buttons:
  328. if _.is_enabled() and _.is_displayed():
  329. loc_buttons_can_use.append(_)
  330. if config_info['loc'] == 'middle':
  331. loc_buttons_can_use[1].click()
  332. if config_info['loc'] == 'right':
  333. loc_buttons_can_use[2].click()
  334. # 设置文本大小
  335. if config_info['str_num'] != 15:
  336. str_buttons = self.driver.find_elements_by_xpath("//*[@class='adui-select-selection__rendered']")
  337. str_buttons_can_use = []
  338. for _ in str_buttons:
  339. if _.is_displayed() and _.is_enabled():
  340. str_buttons_can_use.append(_)
  341. str_buttons_can_use[1].click()
  342. time.sleep(random.uniform(1, 3))
  343. str_num_buttons = self.driver.find_elements_by_xpath("//*[@class='adui-select-dropdown-menu-item']")
  344. str_num_can_use = []
  345. for _ in str_num_buttons:
  346. if _.is_displayed() and _.is_enabled():
  347. str_num_can_use.append(_)
  348. str_num_map = {14: 0, 16: 1, 18: 2, 20: 3, 24: 4, 36: 5}
  349. str_num_can_use[str_num_map[config_info['str_num']]].click()
  350. # 设置文本边距
  351. if config_info['str_distance'][0] != 22 or config_info['str_distance'][1] != 22:
  352. distance_buttons = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  353. distance_buttons_can_use = []
  354. for _ in distance_buttons:
  355. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '22':
  356. distance_buttons_can_use.append(_)
  357. distance_buttons_can_use[0].click()
  358. for i in range(4):
  359. distance_buttons_can_use[0].send_keys(Keys.BACKSPACE)
  360. distance_buttons_can_use[0].send_keys(config_info['str_distance'][0])
  361. distance_buttons_can_use[1].click()
  362. for i in range(4):
  363. distance_buttons_can_use[1].send_keys(Keys.BACKSPACE)
  364. distance_buttons_can_use[1].send_keys(config_info['str_distance'][1])
  365. def set_follow_button(self, button_type):
  366. # 设置关注按钮
  367. # 1.设置button文本内容2.是否加粗3.设置字体颜色,边框颜色,填充色4.设置边距
  368. self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/section/div[4]/div[2]/div/div').click()
  369. if button_type == 1:
  370. input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  371. for _ in input_elements:
  372. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '关注公众号':
  373. _.click()
  374. _.clear()
  375. for i in range(10):
  376. _.send_keys(Keys.BACKSPACE)
  377. _.send_keys('保存阅读记录')
  378. big_elements = self.driver.find_elements_by_xpath('//span[@class="adui-button-content"]')
  379. for _ in big_elements:
  380. if _.text == '加粗' and _.is_displayed() and _.is_enabled():
  381. _.click()
  382. input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  383. for _ in input_elements:
  384. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '07C160':
  385. _.click()
  386. _.send_keys('DE2821')
  387. if button_type == 2:
  388. input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  389. for _ in input_elements:
  390. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '关注公众号':
  391. _.click()
  392. _.clear()
  393. for i in range(10):
  394. _.send_keys(Keys.BACKSPACE)
  395. _.send_keys('点击此处,继续阅读')
  396. big_elements = self.driver.find_elements_by_xpath('//span[@class="adui-button-content"]')
  397. for _ in big_elements:
  398. if _.text == '加粗' and _.is_displayed() and _.is_enabled():
  399. _.click()
  400. input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  401. for _ in input_elements:
  402. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '07C160':
  403. _.click()
  404. _.send_keys('DE2821')
  405. size_buttons = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  406. size_buttons_can_use = []
  407. for _ in size_buttons:
  408. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '28':
  409. size_buttons_can_use.append(_)
  410. size_buttons_can_use[0].click()
  411. for i in range(4):
  412. size_buttons_can_use[0].send_keys(Keys.BACKSPACE)
  413. size_buttons_can_use[0].send_keys('0')
  414. size_buttons_can_use[1].click()
  415. for i in range(4):
  416. size_buttons_can_use[1].send_keys(Keys.BACKSPACE)
  417. size_buttons_can_use[1].send_keys('0')
  418. if button_type == 3:
  419. input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  420. for _ in input_elements:
  421. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '关注公众号':
  422. _.click()
  423. _.clear()
  424. for i in range(10):
  425. _.send_keys(Keys.BACKSPACE)
  426. _.send_keys('点我,继续阅读')
  427. big_elements = self.driver.find_elements_by_xpath('//span[@class="adui-button-content"]')
  428. for _ in big_elements:
  429. if _.text == '加粗' and _.is_displayed() and _.is_enabled():
  430. _.click()
  431. input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  432. for _ in input_elements:
  433. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '07C160':
  434. _.click()
  435. _.send_keys('FFFFFF')
  436. input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  437. for _ in input_elements:
  438. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == 'FFFFFF':
  439. _.click()
  440. _.send_keys('DE2821')
  441. break
  442. size_buttons = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  443. size_buttons_can_use = []
  444. for _ in size_buttons:
  445. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '28':
  446. size_buttons_can_use.append(_)
  447. size_buttons_can_use[0].click()
  448. for i in range(4):
  449. size_buttons_can_use[0].send_keys(Keys.BACKSPACE)
  450. size_buttons_can_use[0].send_keys('0')
  451. size_buttons_can_use[1].click()
  452. for i in range(4):
  453. size_buttons_can_use[1].send_keys(Keys.BACKSPACE)
  454. size_buttons_can_use[1].send_keys('100')
  455. def set_follow_button_(self, config_info):
  456. # 设置关注按钮
  457. # 1.设置button文本内容 2.是否加粗 3.设置字体颜色,边框颜色,填充色 4.设置边距
  458. self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/section/div[4]/div[2]/div/div').click()
  459. # 文本设置
  460. input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  461. for _ in input_elements:
  462. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '关注公众号':
  463. _.click()
  464. _.clear()
  465. for i in range(10):
  466. _.send_keys(Keys.BACKSPACE)
  467. _.send_keys(config_info['content'])
  468. # 文本是否加粗
  469. if config_info['is_bold']:
  470. big_elements = self.driver.find_elements_by_xpath('//span[@class="adui-button-content"]')
  471. for _ in big_elements:
  472. if _.text == '加粗' and _.is_displayed() and _.is_enabled():
  473. _.click()
  474. # 颜色设置
  475. input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  476. input_elements_can_use = []
  477. for _ in input_elements:
  478. if _.is_enabled() and _.is_displayed() and (
  479. _.get_attribute("value") == '07C160' or _.get_attribute("value") == 'FFFFFF'):
  480. input_elements_can_use.append(_)
  481. input_elements_can_use[0].click()
  482. input_elements_can_use[0].send_keys(config_info['color1'])
  483. time.sleep(random.uniform(0.1, 0.5))
  484. input_elements_can_use[1].click()
  485. input_elements_can_use[1].send_keys(config_info['color2'])
  486. time.sleep(random.uniform(0.1, 0.5))
  487. input_elements_can_use[2].click()
  488. input_elements_can_use[2].send_keys(config_info['color3'])
  489. time.sleep(random.uniform(0.1, 0.5))
  490. # 间隙设置
  491. size_buttons = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
  492. size_buttons_can_use = []
  493. for _ in size_buttons:
  494. if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '28':
  495. size_buttons_can_use.append(_)
  496. size_buttons_can_use[0].click()
  497. for i in range(4):
  498. size_buttons_can_use[0].send_keys(Keys.BACKSPACE)
  499. size_buttons_can_use[0].send_keys(config_info['str_distance'][0])
  500. size_buttons_can_use[1].click()
  501. for i in range(4):
  502. size_buttons_can_use[1].send_keys(Keys.BACKSPACE)
  503. size_buttons_can_use[1].send_keys(config_info['str_distance'][1])
  504. def set_text_button(self):
  505. # 设置图文按钮
  506. pass
  507. def send_file_multi(self, layout, err_num=0):
  508. # 有问题,暂时不使用
  509. # 上传文件,文件上传与整体流程切割开
  510. token = re.findall('token=(\d+)', self.driver.current_url)
  511. url_ = 'https://mp.weixin.qq.com/promotion/frame?t=ad_system/common_frame&t1=material_std/material_library&token={}'.format(
  512. token[0])
  513. js = "window.open('{}')".format(url_)
  514. self.driver.execute_script(js)
  515. time.sleep(random.uniform(1, 2))
  516. self.driver.switch_to.window(self.driver.window_handles[-1])
  517. file_set = set()
  518. for v in layout.values():
  519. if isinstance(v, dict):
  520. if 'multi_page' in v.keys() or 'page' in v.keys() or 'movie' in v.keys():
  521. v_name = list(v.keys())[0]
  522. if isinstance(v[v_name], list):
  523. for _ in v[v_name]:
  524. file_set.add(_)
  525. else:
  526. file_set.add(v[v_name])
  527. file_list = list(file_set)
  528. # print(file_list)
  529. # print('file list size', len(file_list))
  530. send_file_sign = self.send_file_limit_num
  531. for _ in file_list:
  532. # print(_)
  533. self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(_)
  534. time.sleep(100)
  535. while True:
  536. # 失败,失败直接退出重跑
  537. # 正在上传数量,如果是正在上传小于限定数量,就不断输入
  538. # 上传完毕,小于限定数量,不断输入,.....已经上传好了,就关闭
  539. time.sleep(1)
  540. send_reslut_content = self.driver.find_element_by_xpath(
  541. '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/div/strong').text
  542. if '失败' in send_reslut_content:
  543. if err_num < 3:
  544. self.send_file(layout, err_num=err_num + 1)
  545. else:
  546. exit()
  547. if '正在上传' in send_reslut_content:
  548. if send_file_sign < len(file_list):
  549. send_num = re.findall('(\d+)', send_reslut_content)[0]
  550. if int(send_num) < self.send_file_limit_num:
  551. # 还可以继续上传
  552. x = send_file_sign
  553. can_send_sign = send_file_sign + (self.send_file_limit_num - send_num)
  554. y = can_send_sign if can_send_sign < len(file_list) else len(file_list)
  555. for i in range(x, y):
  556. send_file_sign = send_file_sign + 1
  557. self.driver.find_element_by_xpath(
  558. '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_list[i])
  559. else:
  560. pass
  561. if '上传成功' in send_reslut_content:
  562. if send_file_sign < len(file_list):
  563. send_num = re.findall('(\d+)', send_reslut_content)[0]
  564. if int(send_num) < self.send_file_limit_num:
  565. # 还可以继续上传
  566. x = send_file_sign
  567. can_send_sign = send_file_sign + (self.send_file_limit_num - send_num)
  568. y = can_send_sign if can_send_sign < len(file_list) else len(file_list)
  569. for i in range(x, y):
  570. send_file_sign = send_file_sign + 1
  571. self.driver.find_element_by_xpath(
  572. '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_list[i])
  573. else:
  574. break
  575. time.sleep(1000)
  576. self.driver.execute_script('window.close();')
  577. def send_file(self, file_path, err_num=0):
  578. # 有问题,暂时不使用
  579. self.driver.execute_script('location.reload();')
  580. self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_path)
  581. def send_file_alone(self, layout):
  582. # 上传文件,单线程
  583. # 上传文件,文件上传与整体流程切割开
  584. token = re.findall('token=(\d+)', self.driver.current_url)
  585. url_ = 'https://mp.weixin.qq.com/promotion/frame?t=ad_system/common_frame&t1=material_std/material_library&token={}'.format(
  586. token[0])
  587. js = "window.open('{}')".format(url_)
  588. self.driver.execute_script(js)
  589. time.sleep(random.uniform(1, 2))
  590. self.driver.switch_to.window(self.driver.window_handles[-1])
  591. file_set = set()
  592. for v in layout.values():
  593. if isinstance(v, dict):
  594. if 'multi_page' in v.keys() or 'page' in v.keys() or 'movie' in v.keys():
  595. v_name = list(v.keys())[0]
  596. if isinstance(v[v_name], list):
  597. for _ in v[v_name]:
  598. file_set.add(_)
  599. else:
  600. file_set.add(v[v_name])
  601. for _ in file_set:
  602. self.send_file(_)
  603. err_num = 0
  604. while True:
  605. time.sleep(1)
  606. send_reslut_content = self.driver.find_element_by_xpath(
  607. '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/div/strong').text
  608. if '上传成功' in send_reslut_content:
  609. break
  610. if '失败' in send_reslut_content:
  611. err_num = err_num + 1
  612. self.send_file(_)
  613. if err_num > 3:
  614. # TODO:退出机制需要考虑一下,tornado返回错误信息
  615. exit()
  616. self.driver.execute_script('window.close();')
  617. self.driver.switch_to.window(self.driver.window_handles[-1])
  618. def set_share_content(self):
  619. # TODO:设置分享标题,文本
  620. pass
  621. def get_layout(self, layout):
  622. # TODO:设置重试机制,然后错误后试错几次,查看是否成功
  623. self.send_file_alone(layout)
  624. advertisement_sign = self.set_advertisement_sign()
  625. self.set_head_assemb(layout[-1])
  626. if -2 in layout.keys():
  627. self.set_background_color()
  628. for _ in range(max(layout.keys()) + 1):
  629. info = layout[_]
  630. info_key = list(info.keys())[0]
  631. info_v = info[info_key]
  632. if info_key == 'page':
  633. self.set_page(info_v)
  634. if info_key == 'content':
  635. self.set_content(info_v)
  636. if info_key == 'follow_button':
  637. self.set_follow_button_(info_v)
  638. time.sleep(100)
  639. return advertisement_sign
  640. if __name__ == '__main__':
  641. logging.basicConfig(
  642. handlers=[
  643. logging.handlers.RotatingFileHandler('./create_ad.log',
  644. maxBytes=10 * 1024 * 1024,
  645. backupCount=5,
  646. encoding='utf-8')
  647. , logging.StreamHandler() # 供输出使用
  648. ],
  649. level=logging.INFO,
  650. format="%(asctime)s - %(levelname)s %(filename)s %(funcName)s %(lineno)s - %(message)s"
  651. )