123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- from wechat_action.login_ad import LogIn
- from selenium import webdriver
- from selenium.webdriver import ActionChains
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.support.wait import WebDriverWait
- from selenium.webdriver import ChromeOptions
- import time
- import random
- import re
- class CreateAd:
- def __init__(self):
- # TODO:创建完页面需要关闭所有页面并回到一开始的页面
- self.driver = LogIn().get_driver_loged()
- self.get_into_create_page()
- self.send_file_limit_num = 8
- def get_into_create_page(self):
- # 进入创建页面
- self.driver.find_element_by_id('material').click()
- WebDriverWait(self.driver, 10).until(lambda driver: driver.find_element_by_class_name('ui-fl-r'))
- time.sleep(random.uniform(0.1, 0.2))
- self.driver.find_element_by_class_name('ui-fl-r').click()
- WebDriverWait(self.driver, 5).until(lambda driver: driver.find_element_by_css_selector(
- '#wxadcontainer > div:nth-child(1) > div:nth-child(2) > div.dialog-1fj_N480ZT > div > div > div:nth-child(1) > div.dialogCardFooter-17KpBD1lgN > button'))
- self.driver.find_element_by_css_selector(
- '#wxadcontainer > div:nth-child(1) > div:nth-child(2) > div.dialog-1fj_N480ZT > div > div > div:nth-child(1) > div.dialogCardFooter-17KpBD1lgN > button').click()
- self.driver.switch_to.window(self.driver.window_handles[-1])
- WebDriverWait(self.driver, 100).until(lambda driver: driver.find_element_by_class_name('addContent-8pexaaAGYy'))
- self.driver.find_element_by_class_name('addContent-8pexaaAGYy').click()
- WebDriverWait(self.driver, 100).until(lambda driver: driver.find_element_by_class_name('topArea-qOwEAeNuIn'))
- def create_layout(self):
- # 创建编排
- pass
- def set_head_assemb(self, info):
- def single_page_set():
- pass
- def multi_page_set():
- self.driver.find_element_by_css_selector(
- '#stage-sidebar > div.topArea-qOwEAeNuIn > div > div:nth-child(2)').click()
- time.sleep(random.uniform(0.1, 0.2))
- page_size = len(info_v)
- if page_size == 3:
- pass
- if page_size == 4:
- self.driver.find_element_by_css_selector(
- '#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()
- if page_size == 6:
- self.driver.find_element_by_css_selector(
- '#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()
- time.sleep(random.uniform(0.1, 0.2))
- self.driver.find_element_by_class_name('imageUploadItem-tA9JX0RWua').click()
- WebDriverWait(self.driver, 1000).until(
- lambda driver: self.driver.find_element_by_class_name('adui-tabs-tab')
- )
- input_elements = self.driver.find_elements_by_tag_name('input')
- input_find_element = None
- for _ in input_elements:
- if '输入关键词搜索素材' in _.get_attribute('placeholder'):
- input_find_element = _
- from selenium.webdriver.common.keys import Keys
- for _ in info_v:
- import os
- file_name = os.path.basename(_)
- print(file_name)
- input_find_element.clear()
- print('clear')
- print(input_find_element.get_attribute('value'))
- time.sleep(5)
- #TODO:input----clear 进行清空有问题
- input_find_element.send_keys(file_name)
- input_find_element.send_keys(Keys.RETURN)
- WebDriverWait(self.driver, 1000).until(
- lambda driver: driver.find_element_by_class_name('imgWrp-3KLtxlD1Db'))
- file_element = self.driver.find_element_by_class_name('imgWrp-3KLtxlD1Db')
- WebDriverWait(self.driver, 1000).until(
- lambda x: (file_element.is_displayed() and file_element.is_enabled()))
- file_element.click()
- time.sleep(random.uniform(1,4))
- time.sleep(1000)
- self.driver.find_elements_by_class_name('adui-tabs-tab')[1].click()
- for i in range(len(info_v)):
- self.driver.find_element_by_xpath("//div[starts-with(@id,'js_multi_upload_material')]/input").send_keys(
- info_v[i])
- info_key = list(info.keys())[0]
- info_v = info[info_key]
- if info_key == 'page':
- single_page_set()
- if info_key == 'multi_page':
- multi_page_set()
- # 设置头部组件
- pass
- def set_page(self):
- # 设置图片模块
- pass
- def set_title(self):
- # 设置标题模块
- pass
- def set_content(self):
- # 输入文本内容
- pass
- def set_follow_button(self):
- # 设置关注按钮
- pass
- def set_set_text_button(self):
- # 设置图文按钮
- pass
- def send_file_multi(self, layout, err_num=0):
- # 上传文件,文件上传与整体流程切割开
- token = re.findall('token=(\d+)', self.driver.current_url)
- url_ = 'https://mp.weixin.qq.com/promotion/frame?t=ad_system/common_frame&t1=material_std/material_library&token={}'.format(
- token[0])
- js = "window.open('{}')".format(url_)
- print(js)
- self.driver.execute_script(js)
- time.sleep(random.uniform(1, 2))
- self.driver.switch_to.window(self.driver.window_handles[-1])
- file_set = set()
- for v in layout.values():
- if isinstance(v, dict):
- if 'multi_page' in v.keys() or 'page' in v.keys() or 'movie' in v.keys():
- v_name = list(v.keys())[0]
- if isinstance(v[v_name], list):
- for _ in v[v_name]:
- file_set.add(_)
- else:
- file_set.add(v[v_name])
- file_list = list(file_set)
- print(file_list)
- print('file list size', len(file_list))
- send_file_sign = self.send_file_limit_num
- for _ in file_list:
- print(_)
- self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(_)
- time.sleep(100)
- while True:
- # 失败,失败直接退出重跑
- # 正在上传数量,如果是正在上传小于限定数量,就不断输入
- # 上传完毕,小于限定数量,不断输入,.....已经上传好了,就关闭
- time.sleep(1)
- send_reslut_content = self.driver.find_element_by_xpath(
- '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/div/strong').text
- if '失败' in send_reslut_content:
- if err_num < 3:
- self.send_file(layout, err_num=err_num + 1)
- else:
- exit()
- if '正在上传' in send_reslut_content:
- if send_file_sign < len(file_list):
- send_num = re.findall('(\d+)', send_reslut_content)[0]
- if int(send_num) < self.send_file_limit_num:
- # 还可以继续上传
- x = send_file_sign
- can_send_sign = send_file_sign + (self.send_file_limit_num - send_num)
- y = can_send_sign if can_send_sign < len(file_list) else len(file_list)
- for i in range(x, y):
- send_file_sign = send_file_sign + 1
- self.driver.find_element_by_xpath(
- '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_list[i])
- else:
- pass
- if '上传成功' in send_reslut_content:
- if send_file_sign < len(file_list):
- send_num = re.findall('(\d+)', send_reslut_content)[0]
- if int(send_num) < self.send_file_limit_num:
- # 还可以继续上传
- x = send_file_sign
- can_send_sign = send_file_sign + (self.send_file_limit_num - send_num)
- y = can_send_sign if can_send_sign < len(file_list) else len(file_list)
- for i in range(x, y):
- send_file_sign = send_file_sign + 1
- self.driver.find_element_by_xpath(
- '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_list[i])
- else:
- break
- time.sleep(1000)
- self.driver.execute_script('window.close();')
- def send_file(self, file_path, err_num=0):
- self.driver.execute_script('location.reload();')
- self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_path)
- def send_file_alone(self, layout):
- # 上传文件,单线程
- # 上传文件,文件上传与整体流程切割开
- token = re.findall('token=(\d+)', self.driver.current_url)
- url_ = 'https://mp.weixin.qq.com/promotion/frame?t=ad_system/common_frame&t1=material_std/material_library&token={}'.format(
- token[0])
- js = "window.open('{}')".format(url_)
- self.driver.execute_script(js)
- time.sleep(random.uniform(1, 2))
- self.driver.switch_to.window(self.driver.window_handles[-1])
- file_set = set()
- for v in layout.values():
- if isinstance(v, dict):
- if 'multi_page' in v.keys() or 'page' in v.keys() or 'movie' in v.keys():
- v_name = list(v.keys())[0]
- if isinstance(v[v_name], list):
- for _ in v[v_name]:
- file_set.add(_)
- else:
- file_set.add(v[v_name])
- for _ in file_set:
- self.send_file(_)
- err_num = 0
- while True:
- time.sleep(1)
- send_reslut_content = self.driver.find_element_by_xpath(
- '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/div/strong').text
- if '上传成功' in send_reslut_content:
- break
- if '失败' in send_reslut_content:
- err_num = err_num + 1
- self.send_file(_)
- if err_num > 3:
- # TODO:退出机制需要考虑一下,tornado返回错误信息
- exit()
- self.driver.execute_script('window.close();')
- self.driver.switch_to.window(self.driver.window_handles[-1])
- def send_moive_spic(self):
- # 特殊情况下,上传本地视频
- pass
- def get_layout(self):
- # 得到编排
- layout = {-1: {'page': '/usr/share/wallpapers/deepin/Beach_by_Samuel_Scrimshaw.jpg'},
- 0: {'title': '这是标题'},
- 1: {'content': '这是文本'},
- 2: 'follow_button-1',
- 3: {'title': '这是标题'},
- 4: {'content': '这是文本'},
- 5: {'page': '/usr/share/wallpapers/deepin/Beach_by_Samuel_Scrimshaw.jpg'},
- 6: {'page': '/usr/share/wallpapers/deepin/Beach_by_Samuel_Scrimshaw.jpg'},
- 7: 'follow_button-2',
- 8: 'follow_button-3',
- }
- # TODO:测试,多图,测试,图文表
- layout = {-1: {'multi_page': ['/usr/share/wallpapers/deepin/Beach_by_Samuel_Scrimshaw.jpg',
- '/usr/share/wallpapers/deepin/Sunset_of_the_Lake_Nam_by_Wang_Jinyu.jpg',
- '/usr/share/wallpapers/deepin/The_Gongga_Mountain_by_wangjinyu.jpg',
- '/usr/share/wallpapers/deepin/Reflection_of_the_Kanas_Lake_by_Wang_Jinyu.jpg']},
- 0: {'page': '/usr/share/wallpapers/deepin/Beach_by_Samuel_Scrimshaw.jpg'}
- }
- # self.send_file_alone(layout)
- self.set_head_assemb(layout[-1])
- for _ in range(max(layout.keys())):
- pass
- time.sleep(100)
- if __name__ == '__main__':
- create_ad = CreateAd()
- create_ad.get_layout()
|