main
HuangHai 5 months ago
parent 9772792d9c
commit 137a0376c0

@ -0,0 +1,23 @@
import time
from Config import *
# 等待
def wait(seconds=0.5):
time.sleep(seconds)
# 登录
def login(page):
# 打开登录页面
page.goto("https://ww1.colorlightcloud.com/login", wait_until="domcontentloaded") # 等待 DOM 加载完成
# 输入用户名
username_input = page.locator("#mat-input-0") # 通过 id 定位用户名输入框
username_input.fill(USERNAME)
# 输入密码
password_input = page.locator("input[type='password']") # 根据实际元素定位密码输入框
password_input.fill(PASSWORD)
# 点击登录按钮
login_button = page.locator("button.color-login-btn") # 通过 class 定位登录按钮
login_button.click()
# 等待登录成功
page.wait_for_url("https://ww1.colorlightcloud.com/home", timeout=60000) # 等待跳转到 home 页面

@ -0,0 +1,13 @@
# 隐藏浏览器
# HEADLESS = True
HEADLESS = False
# 素材的固定文件夹名称
MATERIAL_FOLDER_NAME = '播报充电情况'
# 用户名
USERNAME = 'Ylt5786'
# 密码
PASSWORD = 'DJCtjy064'
# 前缀
PREFIX_NAME = 'Test'
# 视频文件
VIDEO_PATH = r"d:\1.mp4"

@ -0,0 +1,172 @@
from playwright.sync_api import Playwright, sync_playwright
from Ylt.CommonUtil import *
# 切换到节目
def changeJieMu(page):
# 点击“节目”
print("点击节目...")
program_library = page.get_by_text("节目").nth(0) # 通过 text 定位媒体库
program_library.click()
# 等待
wait()
# 排程
def paiCheng(page, keyword, stHour, stMin, stSec, endHour, endMin, endSec):
pc_library = page.get_by_text("排程").nth(0)
pc_library.click()
# 等待
wait()
# 定位目标 <span> 元素
today_span = page.locator("span.mat-button-toggle-label-content", has_text="")
today_span.click()
# 等待
wait()
# 定位目标 <mat-icon> 元素
add_icon = page.locator("mat-icon", has_text="add").nth(1)
# 点击 <mat-icon> 元素
if add_icon.count() > 0: # 确保元素存在
add_icon.click()
# 定位目标 <span> 元素
broadcast_span = page.locator("span.mat-radio-label-content", has_text="插播")
# 点击 <span> 元素
if broadcast_span.count() > 0: # 确保元素存在
broadcast_span.click()
# 等待
wait()
# 点击 + 号
add_icon = page.locator("mat-icon", has_text="add").nth(2)
add_icon.click()
# 等待
wait()
# 等待目标对话框出现
dialog = page.get_by_role('dialog', name='选择节目') # 通过对话框标题定位
dialog.wait_for(state="visible")
# 在对话框内操作
# 定位所有 <span> 元素
spans = dialog.locator("span")
# 获取所有 <span> 元素的文本内容
text_list = spans.evaluate_all("elements => elements.map(span => span.textContent.trim())")
# 找到第一个以 keyword 开头的 <span> 元素
for index, text in enumerate(text_list):
if text.startswith(keyword):
print(f"Found first span with text: {text}")
# 获取元素的句柄并强制点击
span_handle = spans.nth(index).element_handle()
span_handle.evaluate("element => element.click()")
break # 找到第一个后退出循环
for index, text in enumerate(text_list):
if text.startswith("确认"):
print(f"Found first span with text: {text}")
span_handle = spans.nth(index).element_handle()
span_handle.evaluate("element => element.click()")
break
wait()
# 定位目标滑动开关的“滑块”部分
toggle_thumb = page.locator("div.mat-slide-toggle-thumb").nth(0)
# 点击“滑块”部分
toggle_thumb.click()
wait()
# 输入开始时间
input_element = page.locator("input#mat-input-3") # 通过 id 定位
input_element.click()
wait()
# 定位目标 <input> 元素
hour_input = page.locator("input.owl-dt-timer-input").nth(0) # 第一个输入框(小时)
minute_input = page.locator("input.owl-dt-timer-input").nth(1) # 第二个输入框(分钟)
second_input = page.locator("input.owl-dt-timer-input").nth(2) # 第三个输入框(秒)
# 输入时间
hour_input.fill(stHour) # 输入小时
wait()
minute_input.fill(stMin) # 输入分钟
wait()
second_input.fill(stSec) # 输入秒
wait()
# 设定
set_button = page.locator("button.owl-dt-control-button", has_text="Set")
set_button.click()
wait()
# ========================================== 输入结束时间==========================================
time_input = page.locator("input#mat-input-4") # 通过 id 定位
time_input.click()
wait()
hour_input = page.locator("input.owl-dt-timer-input").nth(0) # 第一个输入框(小时)
minute_input = page.locator("input.owl-dt-timer-input").nth(1) # 第二个输入框(分钟)
second_input = page.locator("input.owl-dt-timer-input").nth(2) # 第三个输入框(秒)
# 输入时间
hour_input.fill(endHour) # 输入小时
wait()
minute_input.fill(endMin) # 输入分钟
wait()
second_input.fill(endSec) # 输入秒
wait()
# 定位 <button> 元素
set_button = page.locator("button.owl-dt-control-button", has_text="Set")
set_button.click()
wait()
# 定位目标 <span> 元素
confirm_span = page.locator("span.mat-button-wrapper", has_text="确认")
# 点击 <span> 元素
confirm_span.click()
wait()
# 定位目标 <span> 元素
apply_span = page.locator("span", has_text="应用").nth(0)
apply_span.click()
wait()
def run(playwright: Playwright) -> None:
# 启动浏览器,禁用无头模式
browser = playwright.chromium.launch(headless=HEADLESS)
context = browser.new_context()
page = context.new_page()
try:
# 登录
login(page)
# 切换到节目
changeJieMu(page)
# 排程
paiCheng(page, PREFIX_NAME, '12', '00', '00', '13', '00', '00')
except Exception as e:
print(f"操作过程中发生错误: {e}")
finally:
# 关闭浏览器
print("操作完成,关闭浏览器...")
context.close()
browser.close()
def doPaiCheng():
# 初始化 Playwright
playwright = sync_playwright().start()
# 运行测试
run(playwright)
# 结束 Playwright
playwright.stop()

@ -1,381 +0,0 @@
import time
from playwright.sync_api import Playwright, sync_playwright
# 隐藏浏览器
# HEADLESS = True
HEADLESS = False
# 素材的固定文件夹名称
MATERIAL_FOLDER_NAME = '播报充电情况'
# 用户名
USERNAME = 'Ylt5786'
# 密码
PASSWORD = 'DJCtjy064'
# 前缀
PREFIX_NAME = 'Test'
# 视频文件
VIDEO_PATH = r"d:\1.mp4"
# 等待
def wait(seconds=0.5):
time.sleep(seconds)
# 登录
def login(page):
# 打开登录页面
page.goto("https://ww1.colorlightcloud.com/login", wait_until="domcontentloaded") # 等待 DOM 加载完成
# 输入用户名
username_input = page.locator("#mat-input-0") # 通过 id 定位用户名输入框
username_input.fill(USERNAME)
# 输入密码
password_input = page.locator("input[type='password']") # 根据实际元素定位密码输入框
password_input.fill(PASSWORD)
# 点击登录按钮
login_button = page.locator("button.color-login-btn") # 通过 class 定位登录按钮
login_button.click()
# 等待登录成功
page.wait_for_url("https://ww1.colorlightcloud.com/home", timeout=60000) # 等待跳转到 home 页面
# 删除节目
def delProgram(page, keyword):
# 点击“节目”
changeJieMu(page)
# 定位所有符合条件的 <div> 元素
divs = page.locator("div.content-title")
# 获取所有 <div> 元素中的文本内容
text_list = divs.evaluate_all("elements => elements.map(div => div.textContent.trim())")
# 遍历文本内容,找到包含 keyword 的元素
cnt = 0
# 从后往前遍历
for index in range(len(text_list) - 1, -1, -1):
text = text_list[index]
if keyword in text:
print(f"Found element with text: {text}")
# 点击
divs.nth(index).click()
# 等待
wait()
# 点击删除按钮
delete_icon = page.locator("mat-icon:has-text('delete')").nth(0)
if delete_icon.is_visible():
delete_icon.click() # 点击该元素
# 定位并点击 <button> 元素
confirm_button = page.locator("button:has-text('确定')")
if confirm_button.is_visible():
confirm_button.click() # 点击该元素
cnt = cnt + 1
# 等待
wait()
if cnt > 0:
print("删除" + str(cnt) + "个节目")
else:
print("没有找到需要删除的节目,前缀:" + keyword)
# 删除以前上传的素材
def delMaterial(page):
# 点击“媒体库”
media_library = page.get_by_text("媒体库") # 通过 text 定位媒体库
media_library.click()
# 先删除历史文件,防止垃圾太多
# 点击 MATERIAL_FOLDER_NAME
playback_charge_status = page.get_by_text(MATERIAL_FOLDER_NAME)
playback_charge_status.dblclick()
# 等待
wait()
# 检查 <span> 元素是否存在
if page.locator("span.media-group-name").count() > 0:
# 鼠标移动到第一个 <span> 元素
first_span = page.locator("span.media-group-name").nth(0) # 定位第一个 <span> 元素
first_span.hover() # 鼠标移动到该元素
# 等待
wait()
# 定位并点击外层的 <span> 元素
checkbox_span = page.locator("span.mat-checkbox-inner-container").nth(0) # 定位 <span> 元素
if checkbox_span.is_visible():
checkbox_span.click(force=True) # 强制点击该元素
# 定位并点击 <mat-icon> 元素
delete_icon = page.locator("mat-icon:has-text('delete')").nth(0)
if delete_icon.is_visible():
delete_icon.click() # 点击该元素
# 定位并点击 <button> 元素
confirm_button = page.locator("button:has-text('确定')")
if confirm_button.is_visible():
confirm_button.click() # 点击该元素
# 等待
wait()
print("删除素材完成!")
# 上传文件
def uploadVideo(page, video_path):
# 点击“编辑节目” 点一下编辑节目是为了切换一下页面
jiemu_library = page.get_by_text("编辑节目") # 通过 text 定位编辑节目
jiemu_library.click()
# 点击“媒体库”
media_library = page.get_by_text("媒体库") # 通过 text 定位媒体库
media_library.click()
# 点击 MATERIAL_FOLDER_NAME
playback_charge_status = page.get_by_text(MATERIAL_FOLDER_NAME).nth(0)
playback_charge_status.dblclick()
# 点击“上传”按钮
upload_button = page.get_by_text("上传") # 通过 text 定位上传按钮
upload_button.click()
with page.expect_file_chooser() as fc_info:
page.locator("img").click() # 触发文件选择对话框
file_chooser = fc_info.value
file_chooser.set_files(video_path) # 设置文件路径
# 检查上传成功状态
print("检查上传成功状态...")
while True:
success_message = page.locator("span:has-text('上传素材成功')") # 定位成功消息
if success_message.is_visible(): # 如果成功消息可见,表示上传成功
print("上传成功!")
break
print("上传进行中...")
# 等待
wait()
# 发布节目
def publish(page, title, width, height):
print("点击编辑节目...")
jiemu_library = page.get_by_text("编辑节目") # 通过 text 定位编辑节目
jiemu_library.click()
# 等待
wait()
# 点击 MATERIAL_FOLDER_NAME
print("点击" + MATERIAL_FOLDER_NAME + "...")
playback_charge_status = page.locator("span:has-text('" + MATERIAL_FOLDER_NAME + "')").nth(0) # 定位 <span> 元素
if playback_charge_status.is_visible():
playback_charge_status.click() # 点击该元素
# 点击 <img> 元素
print("点击 <img> 元素...")
img_element = page.locator("img.grid-img").nth(0) # 定位 <img> 元素
if img_element.is_visible():
# 定位目标 <canvas> 元素
print("定位目标 <canvas> 元素...")
canvas_element = page.locator("canvas#proCanvas") # 定位 <canvas> 元素
wait()
# 执行拖动操作
print("执行拖动操作...")
img_element.drag_to(canvas_element) # 将图片拖动到 <canvas> 中
wait()
# 定位并输入文本
print("定位并输入文本...")
input_element = page.locator("input[formcontrolname='contentDisplayName']") # 定位 <input> 元素
if input_element.is_visible():
input_element.fill(title) # 输入文本
else:
print("<input> 元素未找到")
wait()
# 定位并输入数值
print("定位并输入数值...")
input_element = page.locator("input[formcontrolname='width']").nth(0) # 定位 <input> 元素
if input_element.is_visible():
input_element.fill(width) # 输入数值
else:
print("<input> 元素未找到")
wait()
# 定位并输入数值
print("定位并输入数值...")
input_element = page.locator("input[formcontrolname='height']").nth(0) # 定位 <input> 元素
if input_element.is_visible():
input_element.fill(height) # 输入数值
else:
print("<input> 元素未找到")
wait()
# 定位并点击“发布”按钮
print("定位并点击“发布”按钮...")
publish_button = page.locator("div.btn.publish") # 定位 <div> 元素
if publish_button.is_visible():
publish_button.click() # 点击该元素
# 等待
wait(2)
tree_node = page.locator("mat-tree-node.node-custom-style", has_text="充电站")
# 点击 <mat-tree-node> 元素
tree_node.click()
# 点击确定按钮
# 定位目标 <span> 元素
confirm_button = page.locator("span.mat-button-wrapper", has_text="确认")
# 点击 <span> 元素
confirm_button.click()
# 切换到节目
def changeJieMu(page):
# 点击“节目”
print("点击节目...")
program_library = page.get_by_text("节目").nth(0) # 通过 text 定位媒体库
program_library.click()
# 等待
wait()
# 排程
def paiCheng(page, keyword, stHour, stMin, stSec, endHour, endMin, endSec):
pc_library = page.get_by_text("排程").nth(0)
pc_library.click()
# 等待
wait()
# 定位目标 <span> 元素
today_span = page.locator("span.mat-button-toggle-label-content", has_text="")
today_span.click()
# 等待
wait()
# 定位目标 <mat-icon> 元素
add_icon = page.locator("mat-icon", has_text="add").nth(1)
# 点击 <mat-icon> 元素
if add_icon.count() > 0: # 确保元素存在
add_icon.click()
# 定位目标 <span> 元素
broadcast_span = page.locator("span.mat-radio-label-content", has_text="插播")
# 点击 <span> 元素
if broadcast_span.count() > 0: # 确保元素存在
broadcast_span.click()
# 等待
wait()
# 点击 + 号
add_icon = page.locator("mat-icon", has_text="add").nth(2)
add_icon.click()
# 等待
wait()
# 等待目标对话框出现
dialog = page.get_by_role('dialog', name='选择节目') # 通过对话框标题定位
dialog.wait_for(state="visible")
# 在对话框内操作
# 定位所有 <span> 元素
spans = dialog.locator("span")
# 获取所有 <span> 元素的文本内容
text_list = spans.evaluate_all("elements => elements.map(span => span.textContent.trim())")
# 找到第一个以 keyword 开头的 <span> 元素
for index, text in enumerate(text_list):
if text.startswith(keyword):
print(f"Found first span with text: {text}")
# 获取元素的句柄并强制点击
span_handle = spans.nth(index).element_handle()
span_handle.evaluate("element => element.click()")
break # 找到第一个后退出循环
for index, text in enumerate(text_list):
if text.startswith("确认"):
print(f"Found first span with text: {text}")
span_handle = spans.nth(index).element_handle()
span_handle.evaluate("element => element.click()")
break
wait()
# 定位目标滑动开关的“滑块”部分
toggle_thumb = page.locator("div.mat-slide-toggle-thumb").nth(0)
# 点击“滑块”部分
toggle_thumb.click()
wait()
# 输入开始时间
input_element = page.locator("input#mat-input-3") # 通过 id 定位
input_element.click()
wait()
# 定位目标 <input> 元素
hour_input = page.locator("input.owl-dt-timer-input").nth(0) # 第一个输入框(小时)
minute_input = page.locator("input.owl-dt-timer-input").nth(1) # 第二个输入框(分钟)
second_input = page.locator("input.owl-dt-timer-input").nth(2) # 第三个输入框(秒)
# 输入时间
hour_input.fill(stHour) # 输入小时
wait()
minute_input.fill(stMin) # 输入分钟
wait()
second_input.fill(stSec) # 输入秒
wait()
# 设定
set_button = page.locator("button.owl-dt-control-button", has_text="Set")
set_button.click()
wait()
# ========================================== 输入结束时间==========================================
time_input = page.locator("input#mat-input-4") # 通过 id 定位
time_input.click()
wait()
hour_input = page.locator("input.owl-dt-timer-input").nth(0) # 第一个输入框(小时)
minute_input = page.locator("input.owl-dt-timer-input").nth(1) # 第二个输入框(分钟)
second_input = page.locator("input.owl-dt-timer-input").nth(2) # 第三个输入框(秒)
# 输入时间
hour_input.fill(endHour) # 输入小时
wait()
minute_input.fill(endMin) # 输入分钟
wait()
second_input.fill(endSec) # 输入秒
wait()
# 定位 <button> 元素
set_button = page.locator("button.owl-dt-control-button", has_text="Set")
set_button.click()
wait()
# 定位目标 <span> 元素
confirm_span = page.locator("span.mat-button-wrapper", has_text="确认")
# 点击 <span> 元素
confirm_span.click()
wait()
# 定位目标 <span> 元素
apply_span = page.locator("span", has_text="应用").nth(0)
apply_span.click()
wait()
def run(playwright: Playwright) -> None:
# 启动浏览器,禁用无头模式
browser = playwright.chromium.launch(headless=HEADLESS)
context = browser.new_context()
page = context.new_page()
try:
# 登录
login(page)
# 切换到节目
changeJieMu(page)
# 排程
paiCheng(page, PREFIX_NAME, '12', '00', '00', '13', '00', '00')
except Exception as e:
print(f"操作过程中发生错误: {e}")
finally:
# 关闭浏览器
print("操作完成,关闭浏览器...")
context.close()
browser.close()
# 初始化 Playwright
playwright = sync_playwright().start()
# 运行测试
run(playwright)
# 结束 Playwright
playwright.stop()

@ -0,0 +1,6 @@
from Ylt.PaiCheng import doPaiCheng
from Ylt.Upload import doUpload
if __name__ == '__main__':
doUpload()
doPaiCheng()

@ -1,41 +1,5 @@
import time
from playwright.sync_api import Playwright, sync_playwright
# 隐藏浏览器
# HEADLESS = True
HEADLESS = False
# 素材的固定文件夹名称
MATERIAL_FOLDER_NAME = '播报充电情况'
# 用户名
USERNAME = 'Ylt5786'
# 密码
PASSWORD = 'DJCtjy064'
# 前缀
PREFIX_NAME = 'Test'
# 视频文件
VIDEO_PATH = r"d:\1.mp4"
# 等待
def wait(seconds=0.5):
time.sleep(seconds)
# 登录
def login(page):
# 打开登录页面
page.goto("https://ww1.colorlightcloud.com/login", wait_until="domcontentloaded") # 等待 DOM 加载完成
# 输入用户名
username_input = page.locator("#mat-input-0") # 通过 id 定位用户名输入框
username_input.fill(USERNAME)
# 输入密码
password_input = page.locator("input[type='password']") # 根据实际元素定位密码输入框
password_input.fill(PASSWORD)
# 点击登录按钮
login_button = page.locator("button.color-login-btn") # 通过 class 定位登录按钮
login_button.click()
# 等待登录成功
page.wait_for_url("https://ww1.colorlightcloud.com/home", timeout=60000) # 等待跳转到 home 页面
from CommonUtil import *
# 删除节目
@ -377,11 +341,10 @@ def run(playwright: Playwright) -> None:
browser.close()
# 初始化 Playwright
playwright = sync_playwright().start()
# 运行测试
run(playwright)
# 结束 Playwright
playwright.stop()
def doUpload():
# 初始化 Playwright
playwright = sync_playwright().start()
# 运行测试
run(playwright)
# 结束 Playwright
playwright.stop()
Loading…
Cancel
Save