Browse Source

ADD:广告投放 添加人群包交互

cxyu 3 years ago
parent
commit
27085c2ff6

+ 8 - 8
wechat_action/create_ad.py

@@ -31,7 +31,6 @@ class CreateAd:
         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 set_advertisement_sign(self):
         # 设置广告标记
         now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
@@ -363,7 +362,7 @@ class CreateAd:
 
         # 设置文本大小
         if config_info['str_num'] != 15:
-            str_buttons = self.driver.find_elements_by_xpath("//*[@class='adui-select-selection__rendered']")
+            str_buttons = self.driver.find_elements_by_xpath("//*[@class='adui-select-selection-search']")
             str_buttons_can_use = []
             for _ in str_buttons:
                 if _.is_displayed() and _.is_enabled():
@@ -371,7 +370,7 @@ class CreateAd:
             str_buttons_can_use[1].click()
             time.sleep(random.uniform(0.5, 1))
 
-            str_num_buttons = self.driver.find_elements_by_xpath("//*[@class='adui-select-dropdown-menu-item']")
+            str_num_buttons = self.driver.find_elements_by_xpath("//*[@class='adui-select-item-option-content']")
             str_num_can_use = []
             for _ in str_num_buttons:
                 if _.is_displayed() and _.is_enabled():
@@ -689,12 +688,12 @@ class CreateAd:
         try:
             self.send_file_alone(layout)
             advertisement_sign = self.set_advertisement_sign()
-            self.set_head_assemb(layout[-1])
+            self.set_head_assemb(layout[str(-1)])
             if -2 in layout.keys():
                 self.set_background_color()
 
-            for _ in range(max(layout.keys()) + 1):
-                info = layout[_]
+            for _ in range(max([int(x) for x in layout.keys()]) + 1):
+                info = layout[str(_)]
                 info_key = list(info.keys())[0]
                 info_v = info[info_key]
                 if info_key == 'page':
@@ -704,7 +703,7 @@ class CreateAd:
                 if info_key == 'follow_button':
                     self.set_follow_button_(info_v)
 
-            self.set_share_content(layout[-3])
+            self.set_share_content(layout[str(-3)])
             self.driver.execute_script('window.close();')
             # 切回到前一页
             self.driver.switch_to.window(self.driver.window_handles[-1])
@@ -718,9 +717,10 @@ class CreateAd:
                 return self.create_layout(layout, err_num=err_num + 1)
 
     def check_sucess(self):
-        #TODO: 添加一个页面检查,查看落地页是否已经在微信后台创建
+        # TODO: 添加一个页面检查,查看落地页是否已经在微信后台创建
         pass
 
+
 if __name__ == '__main__':
     logging.basicConfig(
         handlers=[

+ 7 - 4
wechat_action/login_ad.py

@@ -30,10 +30,14 @@ class LogIn:
         options = ChromeOptions()
         # 防止selenium快速崩坏
         options.add_argument("--disable-dev-shm-usage")
+        # options.add_experimental_option('excludeSwitches', ['enable-automation'])
+        # prefs = {"profile.managed_default_content_settings.images": 2, 'permissions.default.stylesheet': 2}
+        # options.add_experimental_option("prefs", prefs)
+
         # driver = webdriver.Remote(
-        #     command_executor='http://192.168.7.245:4444/wd/hub',
+        #     command_executor='http://47.114.6.178:4444/wd/hub',
         #     options=options)
-        driver = webdriver.Chrome()
+        driver = webdriver.Chrome(options=options)
         driver.maximize_window()
         return driver
 
@@ -123,6 +127,7 @@ class LogIn:
         # rsp_json = requests.get(url=check_url, cookies=cookie_dict).json()
 
         self.driver.get('https://www.baidu.com')
+        self.driver.get('https://a.weixin.qq.com/client')
         for _ in driver_cookies:
             self.driver.add_cookie(_)
         self.driver.get('https://a.weixin.qq.com/client')
@@ -134,8 +139,6 @@ class LogIn:
         else:
             return True
 
-
-
     def upadte_user_info(self):
         # TODO: 更新 用户相关信息
         #  每次登录就更新一次相关数据,------公众号相关数据,人群报相关数据

+ 84 - 24
wechat_action/send_ad.py

@@ -5,6 +5,7 @@ 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
 
@@ -15,17 +16,21 @@ import random
 class SendAd():
     # TODO:一旦登录之后,driver就不要关闭,然后错误就部分不断重试
 
-
-    def __init__(self, task,login_ad):
+    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_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 select_ad_locations(self):
+    def set_ad_locations(self):
         gg_button = self.driver.find_element_by_class_name(
             'adui-button-hasLeftIcon')
         gg_button.click()
@@ -50,10 +55,12 @@ class SendAd():
             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()
+        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()
+            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))
 
         # 花销设置
@@ -64,9 +71,33 @@ class SendAd():
         next_button = self.driver.find_element_by_css_selector('#plan_next_step')
         next_button.click()
 
-    def set_ad_cost(self):
-        # 投放计划创建
-        # 广告创建
+    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()
@@ -76,7 +107,7 @@ class SendAd():
             citys = self.driver.find_elements_by_class_name('checkbox-1pfRMRqv-R')
             for _ in citys:
                 _.click()
-        if self.task['localtion']=='gzh_big_pic':
+        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':
@@ -84,17 +115,46 @@ class SendAd():
             for _ in citys:
                 _.click()
 
+    def set_ad_cost(self):
+        # 投放计划创建
+        # 广告创建
+
         day_cost = self.driver.find_element_by_css_selector('#budget_input')
-        #TODO: 投放时间,投放位置,投放价格还没有进行设置
-        WebDriverWait(self.driver, 10).until(lambda  x:(day_cost.is_displayed() and day_cost.is_enabled()))
+        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:设置人群
-        #TODO:检查人群是否正常
-        pass
+        # 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):
         # 创意创建
@@ -107,9 +167,8 @@ class SendAd():
         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()))
+        WebDriverWait(self.driver, 1000).until(lambda x: (next_button.is_displayed() and next_button.is_enabled()))
 
         next_button.click()
 
@@ -127,11 +186,12 @@ class SendAd():
             exit()
 
     def run(self):
-        # TODO:出错就截图,然后保存-----在最后一步截图,进行保存
-        self.ad_human_action.check_human_info()
-        self.select_ad_locations()
+        # 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()
-
-

+ 20 - 1
wechat_action/send_ad_idea.py

@@ -218,4 +218,23 @@ class IdeaAction:
         self.push_promotion_page()
 
     def idea_pyq(self):
-        pass
+        print(self.driver.current_url)
+        '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[2]/div[2]/div/div[1]/div[1]/div[2]/form/div[2]/div/div[1]/div[2]/div'
+        WebDriverWait(self.driver, 100).until(
+            lambda driver: self.driver.find_element_by_xpath('//*[@class="ql-editor ql-blank"]'))
+        self.driver.find_element_by_xpath(
+            '//*[@class="ql-editor ql-blank"]'
+        ).send_keys(self.task['idea']['content'])
+
+        time.sleep(random.uniform(0.1, 0.5))
+        self.push_promotion_page()
+        time.sleep(random.uniform(0.1, 0.5))
+        if self.task['idea']['data_show'] == 2:
+            self.driver.find_element_by_xpath(
+                '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[2]/div[2]/div/div[1]/div[1]/div[2]/form/div[5]/div[1]/div/span/span[1]').click()
+            if self.task['idea']['data_show-type'] == 2:
+                time.sleep(random.uniform(0.1, 0.5))
+                WebDriverWait(self.driver, 100).until(lambda driver: self.driver.find_element_by_xpath(
+                    '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[2]/div[2]/div/div[1]/div[1]/div[2]/form/div[5]/div[2]/div/div[1]/label/span[1]'))
+                self.driver.find_element_by_xpath(
+                    '//*[@id="wxadcontainer"]/div[1]/div/div[2]/main/div/div[2]/div[2]/div/div[1]/div[1]/div[2]/form/div[5]/div[2]/div/div[1]/label/span[1]').click()

+ 0 - 1
wechat_api/get_wechat_info.py

@@ -163,7 +163,6 @@ class WechatApi():
                 print(_.text)
             for service_num in range(len(service_names)):
                 _ = service_names[service_num]
-                # TODO:服务商太多可能没法点击----需要拿服务商比较多的号测验一下.
                 if _.text not in service_name_used:
                     choice_service = _
                     choice_service_name = _.text