123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- 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 wechat_action.send_ad_idea import IdeaAction
- from wechat_action.human_ad import HumanAd
- from selenium.webdriver.common.keys import Keys
- from wechat_action.login_ad import LogIn
- from selenium.webdriver import ChromeOptions
- import time
- import random
- class SendAd():
- # TODO:一旦登录之后,driver就不要关闭,然后错误就部分不断重试
- def __init__(self, task, login_ad):
- self.task = task
- # self.task_check()
- self.driver = login_ad.get_driver_loged()
- self.ad_idea_action = IdeaAction(self.driver, task)
- self.ad_human_action = HumanAd(self.driver)
- def set_ad_title(self):
- title_ = self.driver.find_element_by_xpath('//*[@class="input-2lFnByGCRh"]')
- for i in range(40):
- title_.send_keys(Keys.BACKSPACE)
- title_.send_keys(self.task['title'])
- title_.send_keys(Keys.RETURN)
- def set_ad_locations(self):
- gg_button = self.driver.find_element_by_class_name(
- 'adui-button-hasLeftIcon')
- gg_button.click()
- # 设置广告形式
- self.driver.switch_to.window(self.driver.window_handles[-1])
- self.driver.execute_script('''
- window.scroll(100000,0);
- ''')
- WebDriverWait(self.driver, 10).until(
- lambda driver: self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT'))
- # 推广公众号
- wechat_gg = self.driver.find_element_by_css_selector('#PRODUCTTYPE_WECHAT')
- wechat_gg.click()
- localtion = self.task['localtion']
- if localtion == 'gzh_botoom':
- # 默认是这个选项,所以不用进行点选
- pass
- if localtion == 'gzh_movie':
- wechat_localtion = self.driver.find_element_by_id('pos_8')
- wechat_localtion.click()
- if localtion == 'pyq':
- wechat_localtion = self.driver.find_element_by_css_selector(
- '#wxadcontainer > div:nth-child(1) > div > div.content-2-HFBij0Uh > main > div > div:nth-child(3) > div.adui-grid-row > div:nth-child(1)')
- wechat_localtion.click()
- if localtion == 'gzh_big_pic':
- self.driver.find_element_by_xpath(
- '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[3]/div[3]/div[1]/div/button').click()
- time.sleep(0.5)
- self.driver.find_element_by_xpath(
- '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[3]/div[3]/div[3]/ol/li[2]').click()
- time.sleep(random.uniform(0.1, 0.3))
- # 花销设置
- # cost_input = self.driver.find_element_by_css_selector(
- # '#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')
- # cost_input.send_keys('100')
- # 进入下一页
- next_button = self.driver.find_element_by_css_selector('#plan_next_step')
- next_button.click()
- def set_ad_time(self):
- WebDriverWait(self.driver, 10).until(
- lambda driver: self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]'))
- input_elements = self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]')
- input_elements[0].click()
- input_elements[0].send_keys(Keys.RIGHT)
- for i in range(7):
- time.sleep(random.uniform(0.1, 0.2))
- input_elements[0].send_keys(Keys.BACKSPACE)
- for _ in self.task['date_list'][0][1:]:
- time.sleep(random.uniform(0.1, 0.2))
- input_elements[0].send_keys(_)
- time.sleep(random.uniform(0.1, 0.2))
- input_elements[1].click()
- time.sleep(0.1)
- input_elements[1].send_keys(Keys.RIGHT)
- for i in range(7):
- time.sleep(random.uniform(0.1, 0.2))
- input_elements[1].send_keys(Keys.BACKSPACE)
- time.sleep(random.uniform(0.1, 0.2))
- for _ in self.task['date_list'][1][1:]:
- time.sleep(random.uniform(0.1, 0.2))
- input_elements[1].send_keys(_)
- def set_ad_human_location(self):
- WebDriverWait(self.driver, 10).until(
- lambda driver: self.driver.find_element_by_css_selector('#area_input > div > i'))
- self.driver.find_element_by_css_selector('#area_input > div > i').click()
- if self.task['localtion'] == 'gzh_botoom':
- self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
- if self.task['localtion'] == 'gzh_movie':
- citys = self.driver.find_elements_by_class_name('checkbox-1pfRMRqv-R')
- for _ in citys:
- _.click()
- if self.task['localtion'] == 'gzh_big_pic':
- self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
- self.driver.find_element_by_xpath('//*[@id="bid_objective_6"]/span[1]').click()
- if self.task['localtion'] == 'pyq':
- citys = self.driver.find_elements_by_class_name('checkbox-1pfRMRqv-R')
- for _ in citys:
- _.click()
- def set_ad_cost(self):
- # 投放计划创建
- # 广告创建
- day_cost = self.driver.find_element_by_css_selector('#budget_input')
- WebDriverWait(self.driver, 10).until(lambda x: (day_cost.is_displayed() and day_cost.is_enabled()))
- day_cost.send_keys('100')
- self.driver.find_element_by_css_selector('#target_next_step').click()
- def set_ad_human(self):
- # TODO:检查人群是否正常
- self.driver.find_element_by_xpath('//*[@id="test-dmp-container"]/div[2]/span/span[1]').click()
- time.sleep(0.1)
- select_human_element = self.driver.find_element_by_xpath(
- '//*[@id="test-dmp-container"]/div[2]/div[2]/div[1]/div/div[1]/button/span')
- ActionChains(self.driver).move_to_element(select_human_element).click().perform()
- time.sleep(0.4)
- WebDriverWait(self.driver, 100).until(
- lambda x: [_ for _ in self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]') if
- _.get_attribute("placeholder") == '输入名称搜索人群包'])
- human_input = [_ for _ in self.driver.find_elements_by_xpath('//*[@class="adui-input-base"]') if
- _.get_attribute("placeholder") == '输入名称搜索人群包'][0]
- for _ in self.task['human_info']:
- human_input.click()
- human_input.send_keys(_)
- human_input.send_keys(Keys.RETURN)
- time.sleep(random.uniform(0.5, 0.6))
- human_names = self.driver.find_elements_by_xpath('//*[@class="adui-table-cellInner"]')
- for human_name in human_names:
- if human_name.text == _:
- human_name.click()
- for _ in range(len(_) + 1):
- time.sleep(0.1)
- human_input.send_keys(Keys.BACKSPACE)
- self.driver.find_element_by_xpath(
- '//*[@class="adui-button-base adui-button-primary adui-button-small"]').click()
- def set_ad_idea(self):
- # 创意创建
- if self.task['localtion'] == 'gzh_botoom':
- self.ad_idea_action.idea_gzh_bottom()
- if self.task['localtion'] == 'gzh_movie':
- self.ad_idea_action.idea_movie_gzh_movie()
- if self.task['localtion'] == 'gzh_big_pic':
- self.ad_idea_action.idea_big_pic()
- if self.task['localtion'] == 'pyq':
- self.ad_idea_action.idea_pyq()
- next_button = self.driver.find_element_by_class_name('adui-button-primary')
- WebDriverWait(self.driver, 1000).until(lambda x: (next_button.is_displayed() and next_button.is_enabled()))
- next_button.click()
- def push_ad(self):
- # 提交
- time.sleep(random.uniform(3, 4))
- self.driver.find_element_by_class_name('icon-3cAquX1RLZ').click()
- time.sleep(random.uniform(0.5, 1))
- self.driver.find_element_by_class_name('adui-button-primary').click()
- def task_check(self):
- # TODO:检查任务有无输入问题,
- # 需要重点确认输入金额,时间是否正确.从最后页面内容进行再次确认
- if 'master' not in self.task.keys() or 'localtion' not in self.task.keys():
- exit()
- def run(self):
- # self.ad_human_action.check_human_info()
- self.set_ad_locations()
- self.set_ad_time()
- self.set_ad_title()
- self.set_ad_human_location()
- self.set_ad_human()
- self.set_ad_cost()
- self.set_ad_idea()
- self.push_ad()
|