简介
此程序为Python制作 使用selenium模拟用户点击网页
(仅为简单款,个人使用,没什么技术,不适用其他公司,仅为参考使用)
整套程序分为两个程序 一个为给每个门店单独运行生成视频 运行一次到结束会为每个门店生成7条左右视频 一个为门店自动发布视频 运行一次到结束会为每个门店成功发布一条视频
运行环境与准备
- python3.0以上
- 通过pip安装好selenium
- 配置好python的path环境
- 门店名称表格列为txt作导入
- Edge自动化驱动
创建视频
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import random
# 创建 Edge Options 对象
options = Options()
# 设置浏览器启动参数,关闭不必要的日志
options.add_argument('--disable-logging') # 禁用所有日志
options.add_argument('--log-level=3') # 仅显示错误日志
options.add_argument('--ignore-certificate-errors') # 忽略SSL证书错误
options.add_argument('--ignore-ssl-errors=yes') # 忽略SSL错误
#定义浏览器驱动
wd = webdriver.Edge(service=Service(r'填上Edge驱动路径', log_path='NUL'),options=options)
#全局等待
wd.implicitly_wait(10)
#打开网页
wd.get('https://life.douyin.com/p/login')
#登录模块################################################################################################################################
#账号密码登录
element = wd.find_element(By.CLASS_NAME, 'src-pages-Login-components-LoginCard-index-module__to-login--KMNbD--212e2')
element.click()
#输入账号
account_element = wd.find_element(By.CSS_SELECTOR, '.life-core-input-inner__wrapper.life-core-input-inner__wrapper-border.life-core-input-inner__wrapper-size-md > .life-core-input.life-core-input-size-md')
account_element.send_keys("账号")
#切换密码登录
element = wd.find_element(By.CLASS_NAME, 'src-pages-Login-components-Phone-index-module__login-tip--ccjZV--212e2.switch-tip')
element.click()
#输入密码
password_element = wd.find_element(By.CSS_SELECTOR, '.life-core-input-inner__wrapper.life-core-input-inner__wrapper-border.life-core-input-inner__wrapper-size-md.life-core-input-inner__wrapper-add-suffix > .life-core-input.life-core-input-size-md')
password_element.send_keys("密码")
#同意用户协议
element = wd.find_element(By.CSS_SELECTOR, '.life-core-checkbox-icon')
element.click()
#点击登录
element = wd.find_element(By.CSS_SELECTOR, '.life-core-btn.life-core-btn-size-md.life-core-btn-type-primary.life-core-btn-shape-angle.life-core-can-input-grouped.src-pages-Login-components-Phone-index-module__form-item--MUJ4u--212e2')
element.click()
time.sleep(10)
#打开智能创作
wd.get('https://life.douyin.com/p/life_creation/aigc?groupid=1800366705567748')
#关闭弹窗
element = wd.find_element(By.CSS_SELECTOR, '.venus-poptip.venus-poptip_show > .venus-poptip-footer > .venus-button-box > .venus-button.venus-button-xs.venus-button-default')
element.click()
# 读取文本文件
with open(r'C:\Users\abc31\Desktop\抖音来客自动化项目\list.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
# 循环遍历每一行
for index, line in enumerate(lines):
#点击智能创作
input_element = wd.find_element(By.XPATH, '//*[@id="root"]/div/div[3]/div[1]/div[2]/button')
input_element.click()
#打开门店列表
input_element = wd.find_element(By.XPATH, 'id("root")/DIV[1]/DIV[2]/DIV[1]/FORM[1]/DIV[3]/DIV[1]/SPAN[2]/SPAN[1]/DIV[1]')
input_element.click()
#搜索门店
input_element = wd.find_element(By.CSS_SELECTOR, '.byted-input-inner__wrapper.byted-input-inner__wrapper-size-md.byted-input-inner__wrapper-add-prefix.byted-input-inner__wrapper-add-suffix')
input_element.send_keys(line.strip())
time.sleep(2)
#选择门店
input_element = wd.find_element(By.CSS_SELECTOR, 'div.index-module_account-select-item-pc__gGLFy')
input_element.click()
time.sleep(2)
#打开套餐列表
input_element = wd.find_element(By.XPATH, '//*[@id="root"]/div/div[2]/div/form/div[6]/button')
input_element.click()
#搜索团单
element = wd.find_element(By.CSS_SELECTOR, '.byted-input-inner__wrapper.byted-input-inner__wrapper-border.byted-input-inner__wrapper-size-md.byted-input-inner__wrapper-add-suffix')
element.send_keys('【新客特惠-半贴美甲】轻奢简约款式不限')
time.sleep(1)
try:
#检查当前门店有无49团单
empty_element = wd.find_element(By.XPATH, '//*[contains(text(), "暂无可关联的商品")]')
element = wd.find_element(By.CSS_SELECTOR, '.byted-input-inner__wrapper-focus > .byted-input')
element.clear()
element.send_keys('【超值套餐】半贴/全贴+含v建构')
element = wd.find_element(By.XPATH, '//*[contains(text(), "【超值套餐】半贴/全贴+含v建构+")]')
element.click()
print("当前门店未找到49团单 正在改为77团单")
except NoSuchElementException:
print("已选择49团单")
element = wd.find_element(By.XPATH, '//*[contains(text(), "【新客特惠-半贴美甲】轻奢简约款式不限+")]')
element.click()
#确认
input_element = wd.find_element(By.CSS_SELECTOR, '.byted-content-inner-wrapper .byted-btn-type-primary')
input_element.click()
#打开素材库
input_element = wd.find_element(By.CSS_SELECTOR, 'div:nth-child(10) .upload-border')
wd.execute_script("arguments[0].scrollIntoView(true);", input_element)
input_element.click()
#加载元素
WebDriverWait(wd, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.material-select-item .byted-checkbox-icon')))
time.sleep(2)
#滚动加载
for i in range(8):
try:
last_elements = wd.find_elements(By.CSS_SELECTOR, '.material-select-item .byted-checkbox-icon')
if last_elements:
last_element = last_elements[-1]
wd.execute_script("arguments[0].scrollIntoView(true);", last_element)
time.sleep(1) # 等待页面加载新内容
else:
print(f"未找到任何元素,等待更多内容加载")
time.sleep(2)
except Exception as e:
print(f"第 {i+1} 次滚动时发生错误: {e}")
# 找到所有目标元素(假设使用某个通用选择器)
elements = wd.find_elements(By.CSS_SELECTOR, '.material-select-item .byted-checkbox-icon')
# 随机选择 4-6 个元素
num_to_click = random.randint(4, 6)
selected_elements = random.sample(elements, num_to_click)
#点击
for element in selected_elements:
wd.execute_script("arguments[0].scrollIntoView(true);", element)
time.sleep(1)
element.click()
#添加素材
input_element = wd.find_element(By.CSS_SELECTOR, 'button.mr-16')
input_element.click()
#提交
input_element = wd.find_element(By.CSS_SELECTOR, 'button.byted-btn-type-primary')
input_element.click()
# 每次循环结束后的提示
print(f"第 {index + 1} 次循环 {line.strip()}视频生成中")
time.sleep(1) # 等待1秒,可以根据需要调整
# 输出最终提示
print("所有门店视频已经在生产中。")
input('回车关闭程序')
发布视频
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys
#定义浏览器驱动
wd = webdriver.Edge(service=Service(r'C:\Users\abc31\Desktop\selenium\msedgedriver.exe'))
#全局等待
wd.implicitly_wait(5)
#打开网页
wd.get('https://life.douyin.com/p/login')
#登录模块################################################################################################################################
#账号密码登录
element = wd.find_element(By.CLASS_NAME, 'src-pages-Login-components-LoginCard-index-module__to-login--KMNbD--212e2')
element.click()
#输入账号
account_element = wd.find_element(By.CSS_SELECTOR, '.life-core-input-inner__wrapper.life-core-input-inner__wrapper-border.life-core-input-inner__wrapper-size-md > .life-core-input.life-core-input-size-md')
account_element.send_keys("账号")
#切换密码登录
element = wd.find_element(By.CLASS_NAME, 'src-pages-Login-components-Phone-index-module__login-tip--ccjZV--212e2.switch-tip')
element.click()
#输入密码
password_element = wd.find_element(By.CSS_SELECTOR, '.life-core-input-inner__wrapper.life-core-input-inner__wrapper-border.life-core-input-inner__wrapper-size-md.life-core-input-inner__wrapper-add-suffix > .life-core-input.life-core-input-size-md')
password_element.send_keys("密码")
#同意用户协议
element = wd.find_element(By.CSS_SELECTOR, '.life-core-checkbox-icon')
element.click()
#点击登录
element = wd.find_element(By.CSS_SELECTOR, '.life-core-btn.life-core-btn-size-md.life-core-btn-type-primary.life-core-btn-shape-angle.life-core-can-input-grouped.src-pages-Login-components-Phone-index-module__form-item--MUJ4u--212e2')
element.click()
time.sleep(10)
#打开智能创作
wd.get('https://life.douyin.com/p/life_creation/aigc?groupid=1800366705567748')
#关闭弹窗
element = wd.find_element(By.CSS_SELECTOR, '.venus-poptip.venus-poptip_show > .venus-poptip-footer > .venus-button-box > .venus-button.venus-button-xs.venus-button-default')
element.click()
#打开发布列表
element = wd.find_element(By.CSS_SELECTOR, 'div.status-bar:nth-child(1)')
element.click()
with open('list.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
#失败变量
shibai = 0
#成功变量
chenggong = 0
#全部视频变量
quanbu = 0
# 循环遍历每一行
for index, line in enumerate(lines):
#打开门店列表
index_element = wd.find_element(By.CSS_SELECTOR, '.w-full .index-module_detail-indicator__ggGQp')
index_element.click()
#搜索门店
index_element = wd.find_element(By.CSS_SELECTOR, 'div:nth-child(32) .byted-input-inner__wrapper:nth-child(1)')
#检查循环 清空输入框
if index > 0:
index_element = wd.find_element(By.CSS_SELECTOR, 'div:nth-child(32) .byted-input-inner__wrapper:nth-child(1)')
index_element.send_keys(Keys.CONTROL + "a")
index_element.send_keys(Keys.BACKSPACE)
index_element.send_keys(line.strip())
time.sleep(1)
#选择门店
index_element = wd.find_element(By.CSS_SELECTOR, 'div:nth-child(32) .index-module_account-select-item-pc__gGLFy:nth-child(1)')
index_element.click()
#循环发布
for i in range(1, 50):
# 使用 f-string 动态生成 CSS 选择器
selector = f".flex:nth-child({i}) .byted-checkbox-icon"
#快捷发布
element = wd.find_element(By.XPATH, '/html/body/div[17]/div/div[2]/div/div[4]/div/div/div/button[1]')
element.click()
#勾选
element = wd.find_element(By.CSS_SELECTOR, selector)
wd.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
#发布
element = wd.find_element(By.XPATH, '/html/body/div[17]/div/div[2]/div/div[4]/div/div/div[1]/button[1]')
element.click()
print(f"{line.strip()}视频发布中,当前第 {i} 条发布中")
quanbu += 1
time.sleep(5)
#检查是否存在包含 "已上线" 文本的元素
if len(wd.find_elements(By.XPATH, '//*[contains(text(), "已上线")]')) == 0:
print("视频发布失败,正在发布下一条")
shibai += 1
# 如果没有检测到该元素,继续下一次循环
continue
#如果检测到包含 "已上线" 文本的元素,执行新的操作
print("视频发布成功,即将开始下一家发布")
chenggong += 1
break
continue
print(f"总共发布了 {quanbu} 个视频 其中发布成功{chenggong}个 发布失败{shibai}个")
input('回车关闭程序')